Python Program to count the number of vowels in a string


February 2, 2022, Learn eTutorial
1012

In this simple python program, we need to count the number of vowels in a string. It is a number-based python program.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

How do count the vowels in a string?

In this string python program, we need to find the number of vowels present in a string. Vowels are a set of characters which are a, e, i, o, u or A, E, I, O, U. It is a simple program in python where we use a counter to increment when we encounter a vowel while traversing through the string which the user enters. 

For example, let us take the word  'Python Programming' in this string, we have encountered 4 vowels which are o, a, i. 

Let us check the program, Accepting the string from the user using input function, and initialize a variable for vowels counting. Then we open a for loop to check every character in a string, with the help of an if condition, check each character with the set of vowels in lower case and upper case. If the conditions satisfy then increment the vowels by one. finally, print the variable vowels after the for loop which has the vowel count. 

ALGORITHM

STEP 1: Accept the input from the user and save that string in a variable using python programming syntax.

STEP 2: Initialize a count for vowels as zero.

STEP 3: Open a for loop from zero to the length of the string to check each character of the string.

STEP 4: Compare each element of the string with the set of vowels in lower case and upper case using a if condition.

STEP 5: Increment the vowels count by one, every time we reach a vowel.

STEP 6: Print the variable vowel using print statement in python language.

Python Source Code

                                          string=input("Enter a string:")
vowels=0
for i in string:
      if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):
            vowels=vowels+1
print("Number of vowels in the string is :")
print(vowels)
                                      

OUTPUT

Enter a string : Python Programming

Number of vowels in the string is : 4