Python Program to Count the Number of Each Vowel – 2024

Hey guys, in this blog we will see a Python Program to Count the Number of Each Vowel.

Code

# Python Program to Count the Number of Each Vowel

# string of vowels
vowels = 'aeiou'

input_str = input('Enter a string -> ')

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

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

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

print(count)

Output

Enter a string -> Hi my name is Abhishek!!!
{'a': 2, 'e': 2, 'i': 3, 'o': 0, 'u': 0}
Enter a string -> What's the date today???
{'a': 3, 'e': 2, 'i': 0, 'o': 1, 'u': 0}
Enter a string -> Vowels in english vocabulary are aeiou
{'a': 4, 'e': 4, 'i': 3, 'o': 3, 'u': 2}
  • Defined a string of all vowels.
  • Taking a string as input from the user in which we will count the no. of vowels.
  • Using input_str.casefold() to lower the whole string.
  • Then we are initializing a count dictionary in which all vowels will be present as keys and all values will be 0.
  • Then we simply keep on traversing our input_str and if we find any vowel, add 1 to its count in the count dictionary.

Check out our other python programming examples

Leave a Reply

Your email address will not be published. Required fields are marked *