2016-04-05 11 views
-4

ünlüler bir işlev yazma:karakterleri sayar ve bunu bir kullanıcı girdileri bir dizeden karakterleri ve sesli harfleri sayan bir işlev yazma ve fonksiyon ve görüntüler bu çağıran bir rutin yazmak gerekir

$ ./count_all.py 
Enter some words: The sun rises in the East and sets in the West 
13 letters in 47 are vowels. 

neler Bunu yapmanın en iyi yolu olur mu?

+2

olası çift ([dize Python seslilerdir Sayısı] http://stackoverflow.com/questions/19967001/count-vowels- in-dize-piton) – idjaw

cevap

1

okunamaz 1 astar:

import re 
stringToTest = "a9821e89asdi89123o9812378u" 
print(str(len(re.findall(r"a|e|i|o|u", stringToTest, re.IGNORECASE))) + " letters in " + str(len(stringToTest)) + " are vowels") 
#6 letters in 26 are vowels 

okunabilir şekilde

import re 
stringToTest = "a9821e89asdi89123o9812378u" 

stringLength = len(stringToTest) #length of stirng, this is how many characters we have 
regexResult = re.findall(r"a|e|i|o|u", stringToTest, re.IGNORECASE) #match for a or e or i or o or u 

numberVowels = len(regexResult) #our number of vowels is how many regex matches we got 


print(str(numberVowels) + " vowels in " + str(stringLength) + " characters") 

#6 vowels in 26 characters 
İlgili konular