Count Vowels in a String in Python

Previously we have to check a character is a vowel or consonant and check if a string starts with the vowel. Now in this post, we will count vowels in a string in python using various methods. The alphabets ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (in uppercase) and ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels, and remaining alphabets are called consonants.

How to Count Vowels in a String Python

This python program using the for loop to count vowels in a string. We will take a user-defined function to count the number of vowels present in a string. Then, we will take a string while declaring the variables. Finally, call the function and the result will be displayed on the screen.

# Python program to count vowels in a string

def countVowels(string):
    num_vowels=0
    # to count the vowels
    for char in string:
        if char in "aeiouAEIOU":
           num_vowels = num_vowels+1
    return num_vowels

# take input
string = input('Enter any string: ')

# calling function and display result
print('No of vowels =',countVowels(string))

Output for the different input values:-

Enter any string: know program
No of vowels = 3

Enter any string: Python
No of vowels = 1

Enter any string: vowel
No of vowels = 2

We can also write a program in a simple way to count vowels in a string.

def checkVowels(string):
    num_vowels = [each for each in string if each in "aeiouAEIOU"]
    print('No of vowels =',len(num_vowels))

string = input('Enter any string: ')
checkVowels(string)

Count Vowels in a String Python using While Loop

In the above program, we will count vowels in a string using the for loop but in this program, count vowels in a string using the while loop.

# Python program to count vowels in a string using while loop

def countVowels(string):
    count = 0
    num_vowels = 0
    
    # to count the vowels
    while count < len(string):
        if string[count] == "a" or string[count] == "e" or 
            string[count] == "i" or string[count] == "o" or 
             string[count] == "u" or string[count] == "A" or 
              string[count] == "E" or string[count] == "I" or 
               string[count] == "O" or string[count] == "U":
           num_vowels = num_vowels+1
        count = count+1
    return num_vowels

# take input
string = input('Enter any string: ')

# calling function and display result
print('No of vowels =',countVowels(string))

Output:-

Enter any string: CountVowels(string)
No of vowels = 5

Python Program to Count the Number of Each Vowel

This python program also performs the same task but in a different way. In this method, we form a dictionary comprehension with the vowels and increment them when a vowel is encountered. This is a better and efficient way to check the number of each vowel present in a string.

# Python program to count the number of each vowel

def countVowels(string):
    # make it suitable for caseless comparisions
    string = string.casefold()
      
    # make a dictionary with each vowel a key and value 0
    count = {i:0 for i in 'aeiou'}
      
    # to count the vowels
    for char in string:
        if char in count:
            count[char] += 1    
    return count

# take input
string = input('Enter any string: ')

# calling function and display result
print(countVowels(string))

Output for the different input values:-

Enter any string: Know Program
{‘i’: 0, ‘a’: 1, ‘u’: 0, ‘o’: 2, ‘e’: 0}

Enter any string: count vowels in a string
{‘e’: 1, ‘i’: 2, ‘a’: 1, ‘o’: 2, ‘u’: 1}

Here, we use the casefold() method to ignore the cases. Basically, this method returns a lowercased version of the string. In each iteration, we check if the character is in the dictionary keys (True if it is a vowel) and increment the value by 1 if true.

Using a list and a dictionary Comprehension

the above program only uses dictionary comprehension but in this program, we have nested a list comprehension inside a dictionary comprehension to count the vowels present in a string.

# Python program to count the number of each vowel

def countVowels(string):
    # make it suitable for caseless comparisions
    string = string.casefold()
      
    # to count the vowels
    count = {x:sum([1 for char in string if char == x]) for x in 'aeiou'}
    print(count)

# take input
string = input('Enter any string: ')

# calling function
countVowels(string)

Output:-

Enter any string: Python Program
{‘a’: 1, ‘u’: 0, ‘o’: 2, ‘e’: 0, ‘i’: 0}

In this program, the dictionary comprehension runs for all vowel characters, and the list comprehension checks inside the dictionary comprehension if any characters in a string match that particular vowel. Finally, The sum() method calculates the sum of the elements.

Also See:- Print Vowels in a String in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *