How do you find vowels and consonants in python?

In this post, we will write a Python program to check whether the entered character is vowel or consonant.

Python Code

In this program, user is asked to input a character. The program checks whether the entered character is equal to the lowercase or uppercase vowels, if it is then the program prints a message saying that the character is a Vowel else it prints that the character is a Consonant.

# taking user input
ch = input("Enter a character: ")

if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I'
 or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
    print(ch, "is a Vowel")
else:
    print(ch, "is a Consonant")

Output:

How do you find vowels and consonants in python?

Related Posts:

  1. C Program to check Vowel or Consonant
  2. Java Program to check Vowel or Consonant
  3. Python program to check whether the character is Alphabet or not
  4. Python program to check if number is even or odd
  5. Python program to print Hello World

Problem: Write a Python program to count the number of vowels and consonants in a given string.

Example:

String: Pencil Programmer
Vowels: 5
Consonants: 12

To count the number of vowels and consonants in a string, we iterate using a for loop through each character of the string and check if it matches a vowel.

If yes then, we increment the vowel counter otherwise increment the consonant counter.

#define all vowels in a list
vowels = ['a', 'e', 'i', 'o', 'u']

#input a string and transform it to lower case
str = input("Enter a string: ").lower()

#define counter variable for both vowels and consonants
v_ctr = 0
c_ctr = 0

#iterate through the characters of the input string 
for x in str:
    #if character is in the vowel list,
    #update the vowel counter otherwise update consonant counter
    if x in vowels:
        v_ctr += 1
    elif x != ' ':
        c_ctr += 1

#output the values of the counters
print("Vowels: ", v_ctr)
print("Consonants: ", c_ctr)

Output:

Enter a string: Pencil Programmer
Vowels: 5
Consonants: 12

Enter a string: Python
Vowels: 1
Consonants: 5

In the above program, we have defined all vowels of the English alphabet in a list and use the same to check for vowels in the string.

Inside the for loop, we check whether the character (x) of the string is present in the vowels list or not.

If yes then it is a vowel, so we increment the vowel counter (v_ctr += 1) otherwise, we increment the counter of consonants (c_ctr += 1).

Finally, we output both counter’s values at the end of the program.

Python program to count vowels or consonants of the given string

Python program to count vowels or consonants of the given string

Contents

  • 1 Python program to count vowels or consonants of the given string
      • 1.0.1 Python code to count the vowels and consonants using for loop
      • 1.0.2 Python code to count the vowels and consonants using for loop – strlen() function
    • 1.1 Suggested for you
    • 1.2 Related posts:

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

How do you find vowels and consonants in python?
count vowels and consonant

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

#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

  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() 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

  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.

Suggested for you

for loop in Python

while loop in Python

if statements in Python

Operator in Python

data type 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 do you find vowels and consonants in a string in python?

To count the number of vowels and consonants in a string, we iterate using a for loop through each character of the string and check if it matches a vowel. If yes then, we increment the vowel counter otherwise increment the consonant counter.

How do you find the vowels in python?

Method 1: Users can use built-in functions to check whether an alphabet is vowel function in python or not. Step 2: Using built-in python functions like (lower(), upper()), determine whether the input is vowel or consonant. Step 3: If the character is a vowel, it should be printed.

How do you get consonants in python?

Program to Find Consonants in a String in Python The casefold() method returns a string where all the characters are lower case. Also, we use the . fromkeys() method. The fromkeys() method creates a new dictionary from the given sequence of … 'i', 'o', 'u' } value = [1].

How do you identify vowels and consonants?

The difference between vowels and consonants A vowel is a speech sound made with your mouth fairly open, the nucleus of a spoken syllable. A consonant is a sound made with your mouth fairly closed.