All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Vowels And Consonant Program In Python

Last Updated : Mar 11, 2024

Vowels And Consonant Program In Python

In this article we will show you the solution of vowels and consonant program in python, let's look at the vowels and consonants concept in Python. Vowels include the letters "a," "e," "I," "o," and "u".

The remaining letters ('b', 'c', 'd', 'f', etc.) are all consonants. A string can be entered by the user into a Python programme.

The For Loop is then used to count the overall number of consonants and vowels in this string. A length of the string can be obtained using Python String len() function.

Now move to the concept of vowels and consonant program in python.

Step By Step Guide On Vowels And Consonant Program In Python :-

Method - Using for loop

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;
print("The number of vowels:",vowels);
print("\nThe number of consonant:",consonants);
  1. As you can see, we have written some Python code that asks the user for a text and then uses a for loop to count the consonants and vowels of that string.
  2. We initialise the string that the user requested to enter in order to count the consonants and vowels in the very first line of code.
  3. Next, two integer counter variables are declared and initialised with the values 0 for vowels and 0 for consonants, respectively.
  4. The for-loop is then used to count all of the vowels and consonants in the specified text.
  5. Following that, we test for vowels using an if statement. Vowels become vowels + 1 (vowels=vowels+1) if a test expression is true.
  6. If a if-statement is false, the control is transferred to the otherwise part, which executes the instructions there.
  7. The print function is used at the very ending of the code to output actual number of consonants and vowels in the given text.

Method - using for loop – strlen() function

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);
  1. You can see that we have written some Python code here that asks the user to enter a text before using the strlen() function to count the vowels and consonants.
  2. We initialise the string that the user requested be entered in order to count the consonants and vowels at the beginning of the code.
  3. Finally, we apply the str.lower() string function to convert upper case letters to lower case letters after declaring and initialising two integer counter variables with the values int vowels=0 and int consonants=0.
  4. Then, using the vowels & consonants variables, the for-loop is utilized to count all of the consonant and vowel in the given string.
  5. Use an if statement to create a vowel test. Vowels become vowels + 1 if the test expression is true (vowels=vowels+1).
  6. Control shifts to the else part & executes the else part statements when the if-statement returns false.
  7. The computer then shows how many vowels and consonants are in the given string.

Conclusion :-

As a result, we have successfully mastered the Python idea of vowels and consonants.

We also discovered that a user might enter a string into a Python code.

The total number of vowels and consonants in this string is then counted using the For Loop.

The Python String len() method can be used to determine the length of a string.

I hope this article on vowels and consonant program in python helps you and the steps and method mentioned above are easy to follow and implement.