In this article, we will discuss the concept of the Python program to count vowels or consonants of the given string
In this post, we are going to learn how to count the vowels and consonants in the given string in Python programming language
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using for loop in Python language
Program 1
#Python program to count vowel or consonant of the given string str=input("Please enter a string as you wish: "); vowels=0 consonants=0 for i in str: 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;#vowel counter is incremented by 1 else: consonants=consonants+1; #consonant counter is incremented by 1 print("The number of vowels:",vowels); print("\nThe number of consonant:",consonants);
When the above program is executed, it produces the following result
Please enter a string as you wish: python The number of vowels: 1 The number of consonants: 5
Approach
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using strlen() function in Python language
Program 2
#Python program to count vowel or consonant of the given string str=input("Please enter a string as you wish: "); vowels=0 consonants=0 str.lower()#call the lower function to avoid upper case letter for i in str: if(i == 'a'or i == 'e'or i == 'i'or i == 'o'or i == 'u' ): vowels=vowels+1; else: consonants=consonants+1; print("The number of vowels:",vowels); print("\nThe number of consonant:",consonants);
When the above program is executed, it produces the following result
Please enter a string as you wish: Python language The number of vowels: 5 The number of consonants: 10
Approach
if statements in Python
Similar post
C code to check whether the Alphabet is vowel or consonant
C++ code to check whether the Alphabet is vowel or consonant
Python code to check whether the Alphabet is vowel or consonant
Java code to check whether the Alphabet is vowel or consonant
Java program to count vowels and consonants in a string
C++ program to count vowels and consonants in a string
C program to count vowels and consonants in a string
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
View Comments
I believe the code is wrong because python is counting the white space as well. For instance, in your example: Python language: there are 9 consonants not 10, but it is counting all non vowels, including white space. You are also not accounting for the fact that there may be integers in the string, your code would count all non vowels as consonants.
i consider your suggestion
invalid syntax
string_s = input() #Good Morning
vowels = 0
consonante = 0
for i in string_s:
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 += 1
else:
if i != ' ' and i.isdigit() == False:
consonante += 1
print(vowels)
print(consonante)