Task: Write a Python String Program To Count Vowels – This python programming tutorial will explain how to find the number of vowels in a given string. The user will input the string at run time.
We will use simple if statement with logical operators and comprison operators. So that we can check whether a given character in the string is a vowl or not.
To get each character from string one by one we will use a for loop as it is used on collections.
The Source Code of Python String Program To Count Vowels
# Write a Python program to # count vowels in a string # The user will enter String # during program execution # display message to enter string # and get input string str1 = input("Enter a string to find number of vowels:") # declare and set vowel_count variable to zero vowel_count = 0 # get all characters one by one # and check each character for vowel # if vowel increment count1 by 1. for i in str1: # check the conditions for vowels if( i=='A' or i=='a' or i=='E' or i=='e' or i=='I' or i=='i' or i=='O' or i=='o' or i=='U' or i=='u'): vowel_count +=1; # now print vowel_count print("Total vowels in given string are: ", vowel_count)
The output of this Vowel counter program is as follows:
Enter a string to find number of vowels:abcde
Total vowels in given string are: 2
Since there are only two vowels ‘a’ and ‘e’ in the string “abcde”.
Pingback: Python Text File Program To Count Vowels | EasyCodeBook.com