Python Program - Count the Number of Each Vowel
wwgi.wiftidea.com
here's a Python program that counts the number of each vowel in a given string:
# define a function to count the vowels def count_vowels(s): # initialize a dictionary to store the counts counts = {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0} # loop through the string and update the counts for char in s: if char.lower() in counts: counts[char.lower()] += 1 # return the counts return counts # get input from user s = input("Enter a string: ") # call the count_vowels function vowel_counts = count_vowels(s) # print the results print("The number of each vowel in the string is:") for vowel, count in vowel_counts.items(): print("{}: {}".format(vowel, count))
In this program, we first define a function called count_vowels
that takes a string as input and returns a dictionary containing the count of each vowel in the string.
We then use the input()
function to get input from the user, and store it in the variable s
.
We then call the count_vowels()
function with the string s
, and store the result in a variable called vowel_counts
.
Finally, we use a loop to print out the count of each vowel in the string, using the items()
method to iterate over the dictionary. Note that we convert each vowel to lowercase before updating the count, so that the counts are case-insensitive.