Write a program to Count the Number of Vowels in a String in Python
In this tutorial we are going to learn about counting the Number of Vowels in a String in Python.
string=raw_input("Enter 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 are:") print(vowels)
- User must enter a string and store it in a variable.
- The count variable is initialized to zero.
- The for loop is used to traverse through the characters in the string.
- An if statement checks if the character is a vowel or not.
- The count is incremented each time a vowel is encountered.
- The total count of vowels in the string is printed.