Python Program to count Vowels and Consonants in String

In this tutorial, we are going to learn writing python programming to count the number of vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) and consonants (any alphabet other than vowels) in a string.

Problem Statement

For any input string, we have to count the number of vowels that appear and the number of consonants that appear. And returns the number of vowels and consonants as an output of our program.

For example:

Case1: For the input string ‘Python Is Fun’

The output should be the number of vowels that is 3(‘o’, ‘I’, ‘u’) and the number of consonants that is 8(‘P’, ‘y’, ‘t’, ‘h’, ‘n’, ‘s’, ‘F’, ‘n’).

Case 2: For the input string ‘Quescol’

The output should be the number of vowels that is 3(‘u’, ‘e’, ‘o’) and the number of consonants that is 4(‘Q’, ‘s’, ‘c’, ‘l’).

Our logic to count vowels and consonants in the string

  • Firstly, our program takes a string as an input using the python built-in function ‘input()’.
  • Then, we will iterate through each character of the string, and for each vowel we encounter we increment 1 to a variable named ‘vowel’, and do the same with another variable ‘consonant’ when we encounter a consonant during iterating.
  • We need to return the number of vowels in the string and the number of consonants in the string which are already stored in the variables ‘vowel’ and ‘consonant’, as the output of our program.
  • While counting for consonants, we also need to check if the character is alphabetized or not. For this job, we take help from a build-in function ‘isalpha()’.

Algorithm to count vowels and consonants in the string

Step1:  Start

Step2:   Take a string as an input from the user.

Step3: Create two variables to store the number of vowels and consonants and assign the initial value ‘0’ to both of them.

Step4:  Use for loop to iterate through the string.

Step5: if vowel found:

                              vowels +=1

           elif  consonant is alphabet:

                              consonant+=1

Step6: print vowel and consonant

Step7:  Stop

Python program to count vowels and consonants in the string

string = input("Enter a String : ")   
vowels = 0  
consonants = 0 
for i in string:  #string iteration 
    if i in ('a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U'):  
        vowels+=1 
    elif i.isalpha():  
        consonants+=1  
print("Vowels :",vowels,"Consonants:",consonants)

Output:

Enter a String : quescol
Vowels : 3 Consonants: 4

Explanation:

For the input string ‘Quescol’, the string is first iterated through each of character, and as the vowels ‘u’, ‘e’, and ‘o’ are found the variable ‘vowel’ is incremented each time by one. In this case, the ‘vowel’ variable is incremented 3 times from ‘0’.

Similarly, the variable ‘consonants’ will increment itself each time if encounters a consonant. In this case, the ‘consonants’ variable will increment itself 4 times as it found consonants ‘Q’, ‘s’, ‘c’, and ‘l’.

Program 2: Using Python Collections – Counter

This method uses the Counter class from Python’s collections module to count occurrences of each character in the string. It then sums the counts of vowels and consonants separately. This method is efficient for strings where characters repeat often because Counter calculates the frequencies in one pass through the string.

from collections import Counter

string = input("Enter a String: ")

vowel_set = set('aeiouAEIOU')  # A set of vowels, both uppercase and lowercase
count = Counter(string)

vowel_count = sum(count[char] for char in count if char in vowel_set)
consonant_count = sum(count[char] for char in count if char.isalpha() and char not in vowel_set)

print("Vowels:", vowel_count, "Consonants:", consonant_count)

Output

Enter a String: quescoll
Vowels: 3 Consonants: 5

Program 3: Using filter and Lambda

This approach uses Python’s filter function along with lambda functions to filter vowels and consonants. filter applies the lambda to each character in the string and returns an iterator with the items for which the lambda returns True. This method is functional in style and may appeal to those who prefer such a programming approach, though it might not be the most efficient for all use cases.

string = input("Enter a String: ")
vowels = 'aeiouAEIOU'

is_vowel = lambda x: x in vowels
is_consonant = lambda x: x.isalpha() and x not in vowels
vowel_count = len(list(filter(is_vowel, string)))
consonant_count = len(list(filter(is_consonant, string)))

print(f"Vowels: {vowel_count}, Consonants: {consonant_count}")

Output

Enter a String: quescol
Vowels: 3, Consonants: 4