Write a Python program to check character is a Vowel or Consonant with a practical example. As we know, vowels are a, e, i, o, u, and all other characters are consonants.
Python Program to check character is Vowel or Consonant
This Python program allows a user to enter any character. Next, we are using the If Else Statement to check whether the user given character is Vowel or Consonant.
Here, the Python If else statement checks whether the Python character is equal to a, e, i, o, u, A, E, I, O, U. If it is TRUE, it is a Vowel. Otherwise, it’s a Consonant.
ch = input("Please Enter Your Own Character : ")
if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A'
or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U'):
print("The Given Character ", ch, "is a Vowel")
else:
print("The Given Character ", ch, "is a Consonant")

The other option is using the in operator. Here, we used the in operator inside the if-else condition to check whether the given character is in a string of lowercase vowels. If true, it’s a vowel.
TIP: We used the Python lower to convert the user-entered character to lowercase.
ch = input("Enter Character: ")
ch = ch.lower()
if ch in 'aeiou':
print('Vowel')
else:
print('Consonant')
Enter Character: N
Consonant
Enter Character: U
Vowel
Program to check character is Vowel or Consonant using ASCII Values
In this example, we are using ASCII Values to check whether a given character is Vowel or Consonant.
ch = input("Please Enter Your Own Character : ")
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("The Given Character ", ch, "is a Vowel")
elif((ord(ch) >= 97 and ord(ch) <= 122) or (ord(ch) >= 65 and ord(ch) <= 90)):
print("The Given Character ", ch, "is a Consonant")
Please Enter Your Own Character : E
The Given Character E is a Vowel
>>>
Please Enter Your Own Character : l
The Given Character l is a Consonant
Check whether a character is a vowel or consonant using string find()
In the program below, we declared a string with lowercase and uppercase vowel characters. Next, we used the Python find() function to search for a character. If the given character matches any of the string characters, it’s a vowel. Otherwise, it is not. Refer to the Python string.
def vowCon(ch):
str = "AEIOUaeiou"
return str.find(ch) != -1
print('I is ' + str(vowCon('I')))
print('n is ' + str(vowCon('n')))
print('d is ' + str(vowCon('d')))
I is True
n is False
d is False
Also Read