Python

Python Program To Count Vowels Or Consonants Of The Given String4 min read

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

When the above program is executed, it produces the following result

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

When the above program is executed, it produces the following result

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.

2 Comments

Leave a Reply to sobharani_ballavarapu X