Python program to count number of each Vowel

Here we will write a Python program to count number of each vowel in a string, sentence or a paragraph.

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

 

Python program to count number of each vowel in a string:

# Program to count the number of each vowel in a string

# string of vowels
vowels = 'aeiou'

# change this value for a different result
ip_str = 'Hello, have you tried our tutorial section yet?'

# uncomment to take input from the user
#ip_str = input("Enter a string: ")

# 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:

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

That was a simple program, let me know if anything is not clear or for any suggestion.

Leave a Comment