Question
Assumes
is a string of lower case characters.
Write a program that counts up the number of vowels contained in the string
s
. Valid vowels are: 'a
', 'e
', 'i
', 'o
', and 'u
'. For example, if s = 'azcbobobegghakl'
, your program should print:Solution
s='azcbobobegghakl'
count = 0
vowels = "aeiou"
for letter in s:
if letter in vowels:
count += 1
print count
Output
5
Explanation
As the question asks, counting vowels. This will involve "loop". Loops will go through the list and start counting as it found a matching character and printing the final counter/frequency.
Further experiment with function.
def letter_cnt(s):
count = 0
vowels = "aeiou"
for letter in s:
if letter in vowels:
count += 1
return count #becareful with the indentation here, python is very sensitive to this. If I would move this to another 5 spaces, it would print 1 because it is inside the the "if" block instead of the "for" block.
print letter_cnt('azcbobobegghakl')