In this Python program, we will learn how to count the number of each vowel in a given string entered by the user.
Here is the Source code of the program to count the number of each vowel in a string entered by the user.
# Python Program to Count the Number of Each Vowel in a String Entered By the User
# string of vowels
vowels = 'aeiou'
# To Take the Input From User
mystr=input("Enter the String: ")
# make it suitable for caseless comparisions
mystr = mystr.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in mystr:
if char in count:
count[char] += 1
print(count)
Enter the String: Tutorialsrack is an Online Learning Website
{'a': 4, 'e': 4, 'i': 5, 'o': 2, 'u': 1}
In this above program, we used the casefold()
function which returns the string in the lowercase string and fromkeys()
function to create a new dictionary withs its key and value.
Comments