Python program to count and display vowels in a string
Python program to count and display vowels in a string
This python program counts the vowels in a string and displays them on the screen. We can do this in several ways. Here we will discuss a few popular methods to do this efficiently.
Source Code
The following source code first uses the Python For Loop that iterates each character in a string. Inside the For Loop, we have uses If statement that checks whether the character is a vowel (a, e, i, o, u, A, E, I, O, U) or not. If it is true, the value of vowels incremented; otherwise, skip that character.
# Python Program to Count Vowels in a String str1 = input("Input String : ") vowels = 0 for i in str1: 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("Total Number of Vowels are = ", vowels)
Output
Here is the result:

The below source code is another method to display and count the vowels in a string:
# Python program to count and display the number of vowels def vowelCheck(String, Vowels): count = [each for each in String if each in Vowels] print(len(count)) print(count) #Main code #Create string String = "TUTORIALANDEXAMPLE" Vowels = "AaEeIiOoUu" vowelCheck(String, Vowels)
Output
Here is the result:

Explanation
In this method, we have stored all the vowels in a string and then pick every character from the given string and check whether it is in the vowel string or not. The vowel string consists of all the vowels with both cases. If the vowel is encountered, the count gets incremented and stored in a list and finally printed.