Python Program to Count the Number of Each Vowel

To understand this example, you should have the knowledge of the following Python programming topics:


Source Code: Using Dictionary

# Program to count the number of each vowels

# string of vowels
vowels = 'aeiou'

ip_str = 'Hello, have you tried our tutorial section yet?'

# make it suitable for caseless comparisions
ip_str = ip_str.casefold()

# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)

# count the vowels
for char in ip_str:
   if char in count:
       count[char] += 1

print(count)

Output

{'a': 2, 'e': 5, 'i': 3, 'o': 5, 'u': 3}

Here, we have taken a string stored in ip_str. Using the method casefold(), we make it suitable for caseless comparisons. Basically, this method returns a lowercased version of the string.

We use the dictionary method fromkeys() to construct a new dictionary with each vowel as its key and all values equal to 0. This is the initialization of the count.

Next, we iterate over the input string using a for loop.

In each iteration, we check if the character is in the dictionary keys (True if it is a vowel) and increment the value by 1 if true.


Source Code: Using a list and a dictionary comprehension

# Using dictionary and list comprehension

ip_str = 'Hello, have you tried our tutorial section yet?'

# make it suitable for caseless comparisions
ip_str = ip_str.casefold()

# count the vowels
count = {x:sum([1 for char in ip_str if char == x]) for x in 'aeiou'}

print(count)

The output of this program is the same as above.

Here, we have nested a list comprehension inside a dictionary comprehension to count the vowels in a single line.

The dictionary comprehension runs for all vowel characters and the list comprehension inside the dictionary comprehension checks if any characters in the string match that particular vowel.

At the end, a list with 1s is generated for the number of each vowel character. The sum() method is used to calculate the sum of the elements for each list.

However, this program is slower as we iterate over the entire input string for each vowel.


Also Read:

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to return the first N vowels from a given string.

  • Return the first N vowels from the string. If there are fewer than N vowels in the string, return "Not found".
  • For example, for input "Hello World", the output should be 'e', 'o', 'o'.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges