How to count the number of each vowel in a string with Python

2 Answers

0 votes
vowels = 'aeiou'

s = 'python c c++ c# java php javascript'

count_vowels = {}.fromkeys(vowels, 0)

for char in s:
    if char in count_vowels:
        count_vowels[char] += 1

print(count_vowels)


'''
run:

{'i': 1, 'u': 0, 'o': 1, 'e': 0, 'a': 4}

'''

 



answered Jun 19, 2017 by avibootz
0 votes
vowels = 'aeiou'

s = 'python c c++ c# java php javascript'

count_vowels = {x: sum([1 for char in s if char == x]) for x in vowels}

print(count_vowels)


'''
run:

{'u': 0, 'o': 1, 'e': 0, 'a': 4, 'i': 1}

'''

 



answered Jun 19, 2017 by avibootz

Related questions

1 answer 132 views
1 answer 125 views
1 answer 114 views
1 answer 150 views
1 answer 108 views
...