Python Practice › String Programs › Program 13

Vowel count of each word

EasyString Programs · 13 of 106

Write a Python program to count the vowels in each word and show the result as a dictionary.

Advertisement
Input
"We are Learning Python Codding"
Output
{'We': 1, 'are': 2, 'Learning': 3, 'Python': 1, 'Codding': 2}

Approach

  1. A dict comprehension maps each word to its vowel count.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment