Python - Check if Character is Vowel or Consonant


Check if Character is Vowel or Consonant

To check if given character is vowel or consonant in Python, check if the character equals any of the vowel. If it equals, then the given character is a vowel, else a consonant.

Prior to checking vowel or consonant, we can also make sure if the given character is alphabet or not using isalpha() builtin function.

Example

In this example, we write a function to read a character from user, and check if the character is vowel or not.

Before comparing with vowel characters, we convert the given character to lowercase, to reduce the comparisons.

Python Program

ch = input('Enter an alphabet : ')
if ch.isalpha():
    x = ch.lower()
    if x=='a' or x=='e' or x=='i' or x=='o' or x=='u':
        print(ch, 'is a vowel.')
    else:
        print(ch, 'is a consonant.')
else:
    print(ch, 'is not an alphabet.')

Output #1

Enter an alphabet : e
e is a vowel.

Output #2

Enter a character : b
b is a consonant.

Output #3

Enter an alphabet : 8
8 is not an alphabet.

Summary

In this Python Examples tutorial, we learned how to check if given character is a vowel or consonant.