In this tutorial we are going to learn writing python program to check given character is a vowel ( a, e, i, o, u) or any other alphabet other than vowels(consonant) .
Problem statement
For any input character, we have to check whether a character is a vowel or consonant.
For example:
Case 1: If a user is given input a or A
The output should be “Vowel”
As we know that A is a vowel
Case2: If the user is given b or B
The output should be “consonant”
As we know that B is consonant
Our logic to check given character is vowel or consonant
- Our program will take any character as an input from the user
- Then checks if the character taken belongs to vowels or consonants, we achieve this using if-else conditional statements.
Python code to check given character is vowel or consonant
ch = input("Enter a character to check if it's a vowel or consonant: ")
# Check if input is a single alphabet character
if len(ch) == 1 and ch.isalpha():
if ch.lower() in ('a', 'e', 'i', 'o', 'u'):
print("Given character", ch, "is a vowel")
else:
print("Given character", ch, "is a consonant")
else:
print("Please enter a single alphabet character only.")
Output 1:
Enter a character to check if it's a vowel or consonant: o
Given character o is a vowel
Explanation:
As we can see for the character ‘o’ output generated is “Vowel”, which is obvious as o is a vowel and satisfying the ‘if ’ statement
Output 2:
Enter a character to check if it's a vowel or consonant: t
Given character t is a consonant
Explanation:
Similarly, for the character ‘t’ output generated is “consonant”, as ‘t’ is not a vowel and does not satisfy the ‘if ’ statement