In this post, we will write a Python program to count the number of each vowel with detailed explanation and example.

As we all know, a vowel is a set of alphabets containing 5 characters (a, e, i, o, u). Counting the number of vowels present in a given string is a common problem often asked in school and college practicals to develop logical thinking

So today we will see two different ways to solve this problem.

  1. Using Python Dictionary &
  2. Using Dictionary comprehension

Count the Number of Each Vowel Using Python Dictionary

First, let’s see the steps or algorithm to count the number of each vowel in the given string, and then we’ll proceed to the programming part.

Algorithm

  1. Ask the user to enter their string and store it in a variable called my_str.
  2. Transform user input (my_str) in lowercase.
  3. Create a dictionary called vowels where keys are characters of vowels and the values are initially set to zero ({"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}).
  4. Using for-loop iterate through each character (char) of my_str and check:
    if char in vowels then update the vowels dictionary by incrementing vowels[char] += 1.
  5. At the end print vowels dictionary.

From the above algorithm now we know how we can write a program to count the number of each vowel in Python so now it is time to write a program.

Source Code

my_str = input("Enter your string: ")
my_str = my_str.lower()


vowels = {"a":0, "e":0, "i":0, "o":0, "u":0}

for char in my_str:
    if char in vowels:
        vowels[char] += 1

print(f"Count of Vowels in given string: {vowels}")

Output

Enter your string: learn python with allinpython
Count of Vowels in given string: {'a': 2, 'e': 1, 'i': 2, 'o': 2, 'u': 0}

Now let’s modify the above program and rewrite it using dictionary comprehension. it will help us to write a whole code in less number of lines.

Count the Number of Each Vowel Using Dictionary Comprehension

First, let’s see the steps or algorithm to count the number of each vowel in the given string, and then we’ll proceed to the programming part.

Algorithm

  1. The first two steps are the same as above algorithms.
  2. At step-3 we create a string containing all the characters of vowels.
    (vowels = "aeiou").
  3. Using dictionary comprehension do:
    {key: sum([1 for i in my_str if i in key]) for key in vowels} and store it in variable called vowels_dict.
  4. At the end print vowels_dict.

Source Code

my_str = input("Enter your string: ")
my_str = my_str.lower()

vowels = "aeiou"

vowels_dict = {key: sum([1 for i in my_str if i in key]) for key in vowels}

print(f"Count of Vowels in given string: {vowels_dict}")

Output

Enter your string: this is my string     
Count of Vowels in given string: {'a': 0, 'e': 0, 'i': 3, 'o': 0, 'u': 0}

This is all about how we can write a Python program to count the number of each vowel in a given string. I hope this article adds some value to your life thank you for reading. see you in the next article.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment