Home » Python Program » Python program to check given character is vowel or consonant

Python program to check given character is vowel or consonant

Last updated:

Support this post with a reaction:

5,885 views

A Python program to check whether a character is a vowel or consonant determines whether the given alphabet belongs to the vowel group (A, E, I, O, U) or the consonant group. In Python, we can perform this check using if-else conditional statements and the in operator.

Problem statement

Write a Python program to check whether the given character is a vowel or consonant.

The program should:

  • Accept a single character from the user.
  • Check whether the entered character is an alphabet.
  • Identify whether it belongs to the vowels (a, e, i, o, u).
  • Display whether the character is a vowel or consonant.
  • Display an appropriate message if the input is invalid.

For example:

InputOutput
aVowel
BConsonant
EVowel
zConsonant
5Invalid input

Algorithm to Check Vowel or Consonant in Python

  1. Start the program.
  2. Read a character from the user.
  3. Check whether the input contains exactly one alphabet.
  4. If the input is invalid, display an error message.
  5. Otherwise, convert the character into lowercase.
  6. Check whether the character belongs to the vowel group (a, e, i, o, u).
  7. If the condition is true, display “Vowel”.
  8. Otherwise, display “Consonant”.
  9. Stop the program.

Python code to check given character is vowel or consonant

ch = input("Enter a character: ")

if len(ch) == 1 and ch.isalpha():
    if ch.lower() in "aeiou":
        print(ch, "is a Vowel")
    else:
        print(ch, "is a Consonant")
else:
    print("Please enter a single alphabet character.")

Output 1:

Enter a character: a
a is a Vowel

Output 2:

Enter a character: B
B is a Consonant

Output 3:

Enter a character: 5
Please enter a single alphabet character.

Explanation of the Python Program

The program works by checking whether the entered input is a single alphabet and then determining whether it is a vowel or consonant.

Step-by-step logic:

  1. Accept input: Use the input() function to get a character from the user and store it in the variable ch.
  2. Validate the input: Use len(ch) == 1 to check whether the input contains exactly one character.
  3. Check alphabet: Use the isalpha() method to ensure that the entered character is an alphabet.
  4. Convert to lowercase: Use lower() to convert the character into lowercase so that both uppercase and lowercase vowels can be checked.
  5. Check vowel: Use the in operator to check whether the character exists in the string "aeiou".
  6. Display the result: If the character belongs to the vowel group, print “Vowel”. Otherwise, print “Consonant”.
  7. Handle invalid input: If the input is not a single alphabet character, display an appropriate error message.

How Does the Python Vowel Checking Logic Work?

The main condition used in this program is:

if ch.lower() in "aeiou":

Here is how it works:

  • ch.lower() converts the entered character into lowercase.
  • "aeiou" contains all five English vowels.
  • The in operator checks whether the character exists in the given string.
  • If the character is found, the condition returns True.
  • Otherwise, it returns False.

Example:

If the input is E, the lower() method converts it into e. Since e exists in "aeiou", the condition becomes true, and the program prints E is a Vowel.

Time and Space Complexity

ComplexityExplanation
Time ComplexityO(1)
Space ComplexityO(1)