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.
Vowel | Decimal | Hexa-Decimal | Binary |
A | 65 | 0x41 | 01000001 |
E | 69 | 0x45 | 01000101 |
I | 73 | 0x49 | 01001001 |
O | 79 | 0x4F | 01001111 |
U | 85 | 0x55 | 01010101 |
Vowel | Decimal | Hexa-Decimal | Binary |
a | 97 | 0x61 | 01100001 |
e | 101 | 0x65 | 01100101 |
i | 105 | 0x69 | 01101001 |
o | 111 | 0x6F | 01101111 |
u | 117 | 0x75 | 01110101 |
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!