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.
Python Program Without Comments
# ~*~*~* ---EasyCodeBook.com--- ~*~*~* str1 = input("Enter a string to find number of vowels:") vowel_count = 0 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; print("Total vowels in given string are: ", vowel_count)
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”.
You will also like:
- Python File Program Count Characters in Text File
- Python Text File Program To Count Vowels
- Python Program To Show Calendar of a Year
- Python Program to Convert String to Upper Case
- Python Program to convert a string to lower case
- Python Program to Convert String to Title Case
- Python Program to convert string to Opposite case
- Python Program to convert string in Sentence case
Write a C++ Program To Check a Number For Even Odd Using Conditional Operator Ternary Operator
Pingback: Python Text File Program To Count Vowels | EasyCodeBook.com