Python | Count and display vowels in a string
Last Updated :
02 Mar, 2023
In this program, we need to count the number of vowels present in a string and display those vowels. This can be done using various methods. In this article, we will go through few of the popular methods to do this in an efficient manner.
Examples:
In a simple way
Input : Geeks for Geeks
Output :
5
['e', 'e', 'o', 'e', 'e']
This is in a different way
Input : Geeks for Geeks
Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}
Counting vowels: String Way
In this method, we will store all the vowels in a string and then pick every character from the enquired string and check whether it is in the vowel string or not. The vowel string consists of all the vowels with both cases since we are not ignoring the cases here. If the vowel is encountered then count gets incremented and stored in a list and finally printed.
Python3
def Check_Vow(string, vowels):
final = [each for each in string if each in vowels]
print ( len (final))
print (final)
string = "Geeks for Geeks"
vowels = "AaEeIiOoUu"
Check_Vow(string, vowels);
|
Output
5
['e', 'e', 'o', 'e', 'e']
Time complexity: O(n)
Auxiliary Space: O(n)
Counting vowels: Dictionary Way
This also performs the same task but in a different way. In this method, we form a dictionary with the vowels and increment them when a vowel is encountered. In this method, we use the case fold method to ignore the cases, following which we form a dictionary of vowels with the key as a vowel. This is a better and efficient way to check and find the number of each vowel present in a string.
Python3
def Check_Vow(string, vowels):
string = string.casefold()
count = {}.fromkeys(vowels, 0 )
for character in string:
if character in count:
count[character] + = 1
return count
vowels = 'aeiou'
string = "Geeks for Geeks"
print (Check_Vow(string, vowels))
|
Output
{'a': 0, 'e': 4, 'i': 0, 'o': 1, 'u': 0}
Time complexity: O(n)
Auxiliary Space: O(n)
Counting vowels: regex way
We can also use this method to perform this task. We can use the regular expression to perform this task. We use re.findall() method to find all the vowels in string make list with them. We use len on list to find total vowels in string.
Python3
import re
def Check_Vow(string, vowels):
str_list = re.findall(f '[{vowels}]' , string, re.I)
print ( len (str_list))
return str_list
vowels = 'aeiou'
string = "Geeks for Geeks"
print (Check_Vow(string, vowels))
|
Output
5
['e', 'e', 'o', 'e', 'e']
Time complexity: O(n), where n is the length of the input string. The time complexity of re.findall() method is O(n) because it scans the entire string once.
Auxiliary space: O(m), where m is the number of vowels in the input string. The space complexity is proportional to the number of vowels in the input string because we are storing all the matched vowels in a list.
Another approach that could be used to count and display the vowels in a string is to use a combination of the Counter class from the collections module and the filter function.
Here is an example of how you could use this approach to solve the problem:
Python3
from collections import Counter
def count_and_display_vowels(string):
vowels = 'aeiouAEIOU'
vowels_list = filter ( lambda c: c in vowels, string)
count = Counter(vowels_list)
return count
string = "Geeks for Geeks"
print (count_and_display_vowels(string))
|
Output
Counter({'e': 4, 'o': 1})
This code uses the filter function to create a filter object that contains only the vowels in the input string. It then uses the Counter class to count the number of occurrences of each vowel in the filter object. The Counter class is a subclass of dict that is specifically designed for counting the occurrences of elements in a list. It returns a dictionary with the elements as keys and the number of occurrences as values.
Counting vowels: recursive function way
Here we use recursive function to get the length of vowels, vowels in a given string. we recursively call the function until we reach the base condition.
Python3
def Check_Vow(start,string,newlist):
if start = = len (string):
return len (newlist),newlist
if string[start] in [ 'a' , 'e' , 'i' , 'o' , 'u' ]:
newlist.append(string[start])
return Check_Vow(start + 1 ,string,newlist)
string = "Geeks for Geeks"
res = Check_Vow( 0 ,string,[])
print ( * res,sep = '\n' )
|
Output
5
['e', 'e', 'o', 'e', 'e']
Time complexity: O(n)
Auxiliary Space: O(n)
Counting vowels: Using operator.countOf() method
Python3
import operator as op
def Check_Vow(string, vowels):
final = [each for each in string if op.countOf(vowels, each) > 0 ]
print ( len (final))
print (final)
string = "Geeks for Geeks"
vowels = "AaEeIiOoUu"
Check_Vow(string, vowels)
|
Output
5
['e', 'e', 'o', 'e', 'e']
Time Complexity: O(n)
Auxiliary Space: O(n)
Counting vowels: Using reduce() and lambda function:
Algorithm:
- Define a function named ‘check_vow’ which takes two arguments, a string and a string containing all vowels.
- Using the reduce function, create a list of vowels in the given string.
- Print the length of the list of vowels and the list of vowels.
- Call the function with the given string and vowel string.
Python3
from functools import reduce
def check_vow(string, vowels):
vowel_list = reduce ( lambda x, y: x + [y] if y in vowels else x, string, [])
print ( len (vowel_list))
print (vowel_list)
string = "Geeks for Geeks"
vowels = "AaEeIiOoUu"
check_vow(string, vowels)
|
Output
5
['e', 'e', 'o', 'e', 'e']
Time Complexity: O(n), where n is the length of the given string. The reduce function iterates over each character in the string, so the time complexity is linear with respect to the length of the string.
Auxiliary Space: O(m), where m is the number of vowels in the given string. The reduce function creates a list of vowels in the string, which is stored in memory. The space complexity is linear with respect to the number of vowels in the string.
Please Login to comment...