Python Program To Count Vowels Or Consonants Of The Given String – Programming Code Examples
Python

Python Program To Count Vowels Or Consonants Of The Given String3 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

Approach

  1. Declare and initialize two integer counter variable as int vowels=0 and consonants=0;
  2. The user asked to enter a string to count vowels and consonants
  3. A for-loop is used to count total vowels and consonants of the given string
  4. Use an if statement to test vowel if the test expression is true,  vowels become vowels + 1(vowels=vowels+1)
  5. When the if-statement is false, control moves to else part and executes else part statements
  6. Finally, the program displays the number of vowels and consonants of the given string.

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

Approach

  1. Declare and initialize two integer counter variable as int vowels=0 and consonants=0;
  2. The user asked to enter a string to count vowels and consonants
  3. call the str.lower() string function to change upper case lettersto lower case letters
  4. A for-loop is used to count total vowels and consonants of the given string using the vowels and consonants variables
  5. Use an if statement to test vowel if the test expression is true,  vowels become vowels + 1(vowels=vowels+1)
  6. When the if-statement is false, control moves to else part and executes else part statements
  7. Finally, the program displays the number of vowels and consonants of the given string.

Leave a Comment