Python Practice › String Programs › Program 13
Vowel count of each word
Write a Python program to count the vowels in each word and show the result as a dictionary.
Input
"We are Learning Python Codding"
Output
{'We': 1, 'are': 2, 'Learning': 3, 'Python': 1, 'Codding': 2}Approach
- A dict comprehension maps each word to its vowel count.
Solution
text = "We are Learning Python Codding"counts = {w: sum(ch in "aeiouAEIOU" for ch in w) for w in text.split()}print(counts)Output
{'We': 1, 'are': 2, 'Learning': 3, 'Python': 1, 'Codding': 2}Line by line
Line 2
ch in "aeiouAEIOU" is True or False; sum() counts the Trues.
Learn the concept
New to this topic? Read the lesson, then come back.