Python Program to Count the Number of Each Vowel

Vowels Definition:

A vowel is a sound that you utter with your mouth open, allowing air to pass through it, and is represented in writing by the letters ‘a’, ‘e’, I ‘o’, and ‘u’.

Examples:

Example1:

Input:

string = "croyez"

Output:

a : 0
e : 1
i : 0
o : 1
u : 0

Explanation:

Here the letters o, e are vowels and o is occurred once and e is occurred once

Example2:

Input:

string ="Hello this is BTechGeeks"

Output:

a : 0
e : 4
i : 2
o : 1
u : 0

Input:

string ="ELEPHANT"

Output:

a : 1
e : 2
i : 0
o : 0
u : 0

Explanation:

Here the letters e ,a  are vowels and e is repeated twice and a is repeated once.
Here we ignore the case while counting vowels

Count the Number of Each Vowel in Python

Below are the ways to count the number of each vowel in given string in Python :

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using Dictionary

Dictionary:

A colon (:) separates each key from its value, commas separate the objects, and the whole thing is enclosed in curly braces. With just two curly braces, an incomplete dictionary with no things is written as follows:

Values may not be unique inside a dictionary, but keys are. A dictionary’s values may be any data type, but the keys must be immutable data types like strings, numbers, or tuples.

Approach:

We’ve taken a string from the ip str variable. We make it ideal for caseless comparisons by using the form casefold(). This method basically returns a lowercased version of the string.

We create a new dictionary with each vowel as its key and all values equal to 0 using the dictionary method fromkeys(). This is where the count is started.

We then use a for loop to iterate over the input string.

We check whether the character is in the dictionary keys in each iteration (True if it is a vowel) and increment the value by 1 if it is.

Below is the implementation:

# Taking a string which have total vowels
vowelstring = 'aeiou'

given_string = 'Hello this is BTechGeeks'

# making the string suitable for caseless comparisions
given_string = given_string.casefold()
# making a dictionary with keys as vowel and the value as 0
countVowels = {}.fromkeys(vowelstring, 0)

# counting the vowels in given string
for char in given_string:
  # increment the count in dictionary if it is vowel
    if char in countVowels:
        countVowels[char] += 1
# print the count
for key, value in countVowels.items():
    print(key, ":", value)

Output:

a : 0
e : 4
i : 2
o : 1
u : 0

Method #2: Using list and dictionary comprehension in python

List Comprehension:

In Python, list comprehension is a simple and elegant way to generate a new list from an existing one.

An expression is followed by a for statement enclosed in square brackets in a list comprehension.

Dictionary Comprehension:

In Python, dictionary comprehension is a simple and elegant way to construct a new dictionary from an iterable.

Dictionary comprehension is made up of an expression pair (key: value) followed by a for statement enclosed in curly braces{}.

This method produces the same output as the previous one.

  • To count the vowels in a single line, we nested a list comprehension within a dictionary comprehension.
  • The dictionary comprehension checks for all vowel characters, and the list comprehension inside the dictionary comprehension checks to see if any characters in the string fit the vowel.
  • Finally, a list of 1s for the number of vowel characters is produced. For each array, the sum() method is used to calculate the sum of the elements.

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

Below is the implementation:

# Taking a string which have total vowels
vowelstring = 'aeiou'

given_string = 'Hello this is BTechGeeks'

# making the string suitable for caseless comparisions
given_string = given_string.casefold()

# counting the vowels in given string
countVowels = {x: sum([1 for char in given_string if char == x])
               for x in 'aeiou'}
# print the count
for key, value in countVowels.items():
    print(key, ":", value)

Output:

a : 0
e : 4
i : 2
o : 1
u : 0

Related Programs: