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

Output 1:

program to count vowel consonant in string

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’.

Output 2:

python program to count vowel and consonant in string

Explanation:

For the input string ‘Python’, the string is first iterated through each characters, and as the vowel ‘o’ is found the variable ‘vowel’ is incremented by 1, In this case, the ‘vowel’ variable is incremented 1 time from ‘0’.

Similarly, the variable ‘consonants’ will increment itself each time it encounters a consonant. In this case, the ‘consonants’ variable will increment itself 5 times as it found consonants ‘P’, ‘y’, ‘t’, ‘h’, and ‘n’. The characters ‘2’, ‘0’, and ‘3’ are not an alphabet but are digits that is why they are not considered consonants.