In this Python example, you will learn to write a Python Program to Count the Number of Each Vowel in a given string. we will use three different methods to solve this problem.
- Counting vowels Using For loops
- Counting vowelDictionary Comprehension
- Counting vowel Using the Counter Class from the Collections Module
Program to Count the Number of Each Vowel Using For loops
string = input("Enter a string: ")
vowels = ['a', 'e', 'i', 'o', 'u']
count = [0, 0, 0, 0, 0]
for char in string:
if char.lower() in vowels:
count[vowels.index(char.lower())] += 1
for i in range(len(vowels)):
print(vowels[i], ":", count[i])
In this program, we ask a user for an input string and initialize two lists – one for vowels (a, e, i, o, u) and another to store the count of each vowel.
We then use a for loop to iterate through each character in the input string. If the character is a vowel, we use the index method to find the index of that vowel in the vowels list, and then increment the count at that index.
Finally, we use another for loop to print the count of each vowel.
Output
Enter a string: hello
a : 0
e : 1
i : 0
o : 1
u : 0
Program to Count the Number of Each Vowel Using Dictionary Comprehension
string = input("Enter a string: ")
vowels = ['a', 'e', 'i', 'o', 'u']
count = {vowel: 0 for vowel in vowels}
for char in string:
if char.lower() in vowels:
count[char.lower()] += 1
for vowel, count in count.items():
print(vowel, ":", count)
In this program, we take an input string from the user and initialize a dictionary to store the count of each vowel. We use dictionary comprehension to create the dictionary, with the keys being the vowels and the values initialized to 0.
We use a for loop to loop through each character in the input string. If the character is a vowel, we increase the count by 1 for that vowel in the dictionary.
Finally, we use another for loop to print the count of each vowel by iterating through the items of the dictionary.
Output
Enter a string: hello
a : 0
e : 1
i : 0
o : 1
u : 0
Program to Count the Number of Each Vowel Using the Counter Class from the Collections Module
from collections import Counter
string = input("Enter a string: ")
vowels = ['a', 'e', 'i', 'o', 'u']
count = Counter(char.lower() for char in string if char.lower() in vowels)
for vowel in vowels:
print(vowel, ":", count[vowel])
In this program, we import the Counter class from the collections module, which allows us to count the frequency of elements in a list. We ask a user to input a string from the user and initialize a list of vowels. We then use list comprehension to create a list of all the vowels in the input string. We pass this list to the Counter class to get a dictionary with the count of each vowel.
Finally, we use a for loop to print the count of each vowel by accessing the value for each key in the dictionary.
Output
Enter a string: America
a : 2
e : 1
i : 1
o : 0
u : 0
Conclusion
These three programs use different approaches to count the number of each vowel in a string. Program 1 uses lists and for loops, Program 2 uses dictionary comprehension, and Program 3 uses the Counter class from the collections module. All three programs achieve the same result, but use different techniques and syntax to do so.