Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program to Count the Number of Vowels in a String

Improve Article
Save Article
  • Last Updated : 06 Mar, 2023
Improve Article
Save Article

In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. So our task is to calculate how many vowels are present in each word of a sentence. So let us first design a simple approach that we will be following in order to accomplish the above task.

Examples:

Input: GeeksforGeeks is a computer science portal for geeks.
Output: 
GeeksforGeeks:4
is: 1
computer: 3
science: 3
portal: 2
geeks: 2
Explanation: The vowels are 'a','e','i','o','u'. The count of vowels in each word is given.

Method #1: The approach seems simple. One thing you might be wondering after reading the approach is how we split the sentence into words. The split() function comes into play for this task. 

Owing to the fact, that now you have understood the approach to solve the problem, we shall now proceed towards the program.

Python3




# Sentence
string = "Hulkmaniacs are running wild in the whole world"
# Sentence splitted up into words
wordList = string.split()
# Defining vowels for uppercase and lowercase
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
#Traversing every word in wordList
for word in wordList:
    vowelCount = 0
    #Traversing every character of the word
    for i in range(0, len(word)):
        # If the word contains a vowel then vowelCount adds by 1
        if word[i] in vowels:
            vowelCount += 1
    print("The word is", word, "and it contains", vowelCount, "vowels in it")

Output

The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

Complexity analysis:

  • Time complexity: O(n*m), where ‘n’ is the number of words in the sentence and ‘m’ is the average length of the words. We are using two nested loops: one to traverse each word in the sentence and the other to traverse each character of the word
  • Auxiliary Space: O(n+m), Where the wordList array takes O(n) space and the vowels array takes O(m) space, so the total space complexity is O(n + m).

Method #2 : Using operator.countOf() method

Python3




import operator as op
# Sentence
string = "Hulkmaniacs are running wild in the whole world"
# Sentence splitted up into words
wordList = string.split()
# Defining vowels for uppercase and lowercase
vowels = 'aeiouAEIOU'
# Traversing every word in wordList
for word in wordList:
    vowelCount = 0
    # Traversing every character of the word
    for i in range(0, len(word)):
        # If the word contains a vowel then vowelCount adds by 1
        if op.countOf(vowels, word[i]) > 0:
            vowelCount += 1
    print("The word is", word, "and it contains", vowelCount, "vowels in it")

Output

The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

Complexity analysis:

  • Time complexity: O(n*m), where ‘n’ is the number of words in the sentence and ‘m’ is the average length of the words. We are using two nested loops: one to traverse each word in the sentence and the other to traverse each character of the word
  • Auxiliary Space: O(n+m), Where the wordList array takes O(n) space and the vowels array takes O(m) space, so the total space complexity is O(n + m).

Method #2 : Using re module:

Python3




import re
#function  to count vowels
def count_vowels(s):
  l=s.split()
  #splitting string in list of words
  for i in l:
    #using the findall function in re to find the count of vowels
    print("The word is",i, "and it contains", len(re.findall(r'[aeiouAEIOU]',i)), "vowels in it")
#input
s="Hulkmaniacs are running wild in the whole world"
#function call
count_vowels(s)

Output

The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

Time Complexity: O(n)
Auxiliary Space: O(n)

Method#4: Using Recursive method.

Algorithm:

  1. Define a function count_vowels_recursive that takes in a list l and an optional index idx set to 0 by default.
  2. Check if the idx is equal to the length of the list l. If so, return None and terminate the function.
  3. Otherwise, retrieve the word at the idxth index of the list l.
  4. Create a string vowels containing all the vowels in upper and lower case.
  5. Count the number of vowels in the current word by iterating over its characters and incrementing a count variable each time a vowel is encountered.
  6. Print a message displaying the current word and the number of vowels it contains.
  7. Call the count_vowels_recursive function recursively with an incremented idx.
  8. Define a function count_vowels that takes in a string s.
  9. Split the string into a list of words l.
  10. Call the count_vowels_recursive function with the list of words l.
  11. Define a string s containing a sentence.
  12. Call the count_vowels function with the string s.

Python3




def count_vowels_recursive(l, idx=0):
    if idx == len(l):
        return
     
    word = l[idx]
    vowels = "aeiouAEIOU"
    count = 0
    for char in word:
        if char in vowels:
            count += 1
    print("The word is", word, "and it contains", count, "vowels in it")
     
    count_vowels_recursive(l, idx + 1)
 
def count_vowels(s):
    l = s.split()
    count_vowels_recursive(l)
 
s = "Hulkmaniacs are running wild in the whole world"
count_vowels(s)
#this code contributed by tvsk

Output

The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

Time Complexity:
The algorithm visits each character of each word in the input string, counting the number of vowels. Therefore, the time complexity of the algorithm is proportional to the number of characters in the input string. In the worst case, where every character in the string is a vowel, the time complexity would be O(n), where n is the length of the string.

Auxiliary Space:
The algorithm uses a constant amount of space for each recursive call, namely O(1) space for storing the word, the vowels string, and the count variable. The space complexity of this algorithm is proportional to the number of recursive calls. In the worst case, where the input string contains a single word with n characters, the space complexity would be O(n), where n is the length of the input string.

Method #5: Using the list and filter() function: 

1. Define the function with input parameter s.
2. Initialize a string variable called “vowels” with all the vowels in it.
3. Split the input string into a list of words called “words” using the split() method.
4. Iterate over each word in “words”:
    a. Count the number of vowels in the current word by filtering the vowels from the word and finding the length of the resulting list.
    b. Print the word and its vowel count using an f-string.
5. End the function.
 

Python3




def count_vowels(s):
    vowels = 'aeiouAEIOU'
    words = s.split()
    for word in words:
        vowel_count = len(list(filter(lambda char: char in vowels, word)))
        print(f"The word is {word} and it contains {vowel_count} vowels in it")
 
# Example usage
s = "Hulkmaniacs are running wild in the whole world"
count_vowels(s)
#This code is contributed by Jyothi pinjala.

Output

The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

Time complexity:

Splitting the input string into words has a time complexity of O(n), where n is the length of the input string.
Iterating over each word and counting the vowels in each word has a time complexity of O(m), where m is the length of the longest word in the input string.
The filter function used to count the vowels has a time complexity of O(k), where k is the number of characters in the word being filtered.
Therefore, the overall time complexity of the function is O(nmk).
Auxiliary Space:

The function uses additional memory to store the “vowels” string, the “words” list, and the “vowel_count” variable for each word.
The space complexity of the function is O(n+m+k), where n is the length of the input string, m is the length of the longest word in the input string, and k is the number of characters in the word being filtered.

Method #6: Using reduce()

  1. Import the reduce function from the functools module.
  2. Define a string of words and split it into a list of words using the split() function.
  3. Define a string of vowels.
  4. Iterate through each word in the list of words.
  5. Use the map() function to count the number of each vowel in the current word and return a list of counts.
  6. Use the reduce() function to sum up the counts of all the vowels in the current word.
  7. Print the current word and the total count of vowels in the word.
  8. Repeat steps 5-7 for all the words in the list.

Python3




# Using the reduce() function
from functools import reduce
 
string = "Hulkmaniacs are running wild in the whole world"
string = string.split()
# Define the vowels
vowels = "aeiouAEIOU"
 
# Count the number of vowels in the string using reduce()
for word in string:
    vowelCount = reduce(lambda x, y: x+y, map(word.count, vowels))
    print(f"The word is {word} and it contains {vowelCount} vowels in it")
# This code is contributed by Vinay Pinjala.

Output

The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

Time Complexity: The time complexity of the map() function is O(n), and the time complexity of the reduce() function is O(n^2). Therefore, the time complexity of this algorithm is O(n^2).

Space Complexity: The space complexity of this algorithm is O(n), as we are storing the list of words and the string of vowels in memory.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!