Python Program to Check Vowel or Consonant

Python program to check vowel or consonant | In this article, we have to develop a program to check given character vowels or consonants using many methods. The characters A, E, I, O, U (uppercase and lowercase) are called vowels. Except all other characters are called consonants.

Python Program to Check Whether a Character is Vowel or Consonant

We use the if-else statement to check character is vowel or consonant. We will give the characters. Then, check whether a character is a vowel or consonant using the if-else statement. Finally, print the result value.

# Python program to check character is vowel or consonant

# input from user
ch = input('Enter any character: ')

# check vowel or constant and display result
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 for the different input values:-

Enter any character: a
a is a Vowel

Enter any character: g
g is a Consonant

Enter any character: I
I is a Vowel

Enter any character: T
T is a Consonant

Vowels and Consonant Program in Python

We can also use the built-in function to check if a character is vowels and consonants in python. Check vowel and consonant using the upper(), lower() function, and if-elif-else statement.

# Python program to check character is vowel or consonant

# input from user
l = input('Enter any character: ')

# check vowel or constant and display result
if l.upper() in ('A', 'E', 'I', 'O', 'U'):
    print(l, "is a Vowel")
elif l.lower() in ('a', 'e', 'i', 'o', 'u'):
    print(l, "is a Vowel")
else:
    print(l, "is a Consonant")

Output:-

Enter any character: E
E is a Vowel

Check Vowels and Consonants using switch case

# Python program to check character is vowel or consonant

def isVowel(ch):  #user-defined function
    switcher = {
        'a': "Vowel",
        'e': "Vowel",
        'i': "Vowel",
        'o': "Vowel",
        'u': "Vowel",
        'A': "Vowel",
        'E': "Vowel",
        'I': "Vowel",
        'O': "Vowel",
        'U': "Vowel"
    }
    return switcher.get(ch, "Consonant")
 
# input from user
ch = input('Enter any character: ')

# calling function and display result
print(ch,'is a '+isVowel(ch))

Output:-

Enter any character: r
r is a Consonant

using the ASCII values

In ASCII these are the respective values of every vowel both in lower and upper cases.

VowelDecimalHexa-DecimalBinary
A650x4101000001
E690x4501000101
I730x4901001001
O790x4F01001111
U850x5501010101
VowelDecimalHexa-DecimalBinary
a970x6101100001
e1010x6501100101
i1050x6901101001
o1110x6F01101111
u1170x7501110101

Check Whether a character is Vowel or Consonant using ASCII values

# Python code to check character is vowel or consonant

# input from user
ch = input('Enter any character: ')

# check vowel or constant and display result
if(ord(ch) == 65 or ord(ch) == 69 or ord(ch) == 73 or ord(ch) == 79 
                  or ord(ch) == 85 or ord(ch) == 97 or ord(ch) == 101 
                   or ord(ch) == 105 or ord(ch) == 111 or ord(ch) == 117):
    print(ch, "is a Vowel")
else:
    print(ch, "is a Consonant")

Output:-

Enter any character: O
O is a Vowel

Also See:- Check If String Starts with Vowel in Python, Print Vowels in a String in Python, & Count Vowels in a String in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *