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
Python code to count the vowels and consonants using for loop
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: Write a program to count the number of vowels and consonants in a given string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#Python program to count vowel or consonant of the given string def letters(input): return ''.join([c for c in input if c.isalpha()]) str=input("Please enter a string as you wish: "); str = letters(str) 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
1 2 3 4 5 |
Please enter a string as you wish: python The number of vowels: 1 The number of consonants: 5 |
This code is a simple Python program that counts the number of vowels and consonants in a string entered by the user. Here is a more detailed explanation of how it works:
The letters
function is defined to remove all non-alphabetic characters from the input string. It does this by using a list comprehension to create a list of all the alphabetic characters in the input string, and then joining them into a single string using the join
method.
The user is prompted to enter a string, and the input is passed to the letters
function to remove any non-alphabetic characters.
Two variables, vowels
and consonants
, are initialized to 0. These variables will be used to keep track of the number of vowels and consonants in the string.
A for loop iterates over each character in the string. For each character, an if-else statement is used to determine if it is a vowel or a consonant. If the character is a vowel, the vowels
counter is incremented by 1. If it is a consonant, the consonants
counter is incremented by 1.
After the for loop completes, the final count of vowels and consonants is printed to the console.
Python code to count the vowels and consonants using for loop – strlen() function
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using strlen()
Program 2: Write a program to count the number of vowels and consonants in a given string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#Python program to count vowel or consonant of the given string def letters(input): return ''.join([c for c in input if c.isalpha()]) str=input("Please enter a string as you wish: "); str = letters(str) 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
1 2 3 4 5 |
Please enter a string as you wish: Python language The number of vowels: 5 The number of consonants: 10 |
This code is similar to the previous code, with one difference: it converts the string to lower case before counting the vowels and consonants. This is done using the lower
method, which converts all the characters in the string to lower case.
The reason for doing this is to avoid double-counting vowels and consonants if the user enters a string with mixed case. For example, if the user enters “Hello”, the letter “H” would be counted as a consonant in the previous version of the code, but with the lower
method it will be converted to “h” and counted as a vowel.
Other than this change, the code works in the same way as the previous version. It defines a function to remove non-alphabetic characters from the input string, prompts the user to enter a string, removes non-alphabetic characters, and then uses a for loop and an if-else statement to count the number of vowels and consonants in the string. Finally, it prints the results to the console.
i strongly believe that it is wrong code because here it shows non-vowels all are consonants(including white spaces)
Thanks for your attentions. I fixed the code.