Python Practice › If-Else Programs › Program 33
Check a consonant
Write a Python program to check whether a letter is a consonant.
Input
"B", "e", "7"
Output
B is a consonant e is not a consonant 7 is not a consonant
Approach
- A consonant is a letter (
isalpha()) that is not a vowel.
Solution
for ch in ["B", "e", "7"]: if ch.isalpha() and ch.lower() not in "aeiou": print(ch, "is a consonant") else: print(ch, "is not a consonant")Output
B is a consonant e is not a consonant 7 is not a consonant
Line by line
Line 2
Checking isalpha() first stops digits and symbols from being called consonants.
Learn the concept
New to this topic? Read the lesson, then come back.