In the Python Program, we will learn how to count the total number of vowels and consonants in a string.
Here is the source code of the program to count the total number of vowels and consonants in a string.
# Python Program to Count the Total Number of Vowels and Consonants in a String
# Take the input from the user
str1 = input("Enter the String: ")
vowels = 0
consonants = 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
else:
consonants = consonants + 1
print("\nTotal Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)
Enter the String: Tutorialsrack is an Online Learning Website
Total Number of Vowels in this String = 16
Total Number of Consonants in this String = 27
Program 2: Python Program to Count the Total Number of Vowels and Consonants in a String Using For Loop and lower() function
Python Program to Count the Total Number of Vowels and Consonants in a String Using For Loop and lower() function
# Python Program to Count the Total Number of Vowels and Consonants in a String
# Take the Input from the User
str1 = input("Enter the String: ")
vowels = 0
consonants = 0
# Make string to Lowercase
str1=str1.lower()
for i in str1:
if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'):
vowels = vowels + 1
else:
consonants = consonants + 1
print("\nTotal Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)
Enter the String: Tutorialsrack is an Online Learning Website
Total Number of Vowels in this String = 16
Total Number of Consonants in this String = 27
Comments