Python Practice › String Programs › Program 6
Vowel or consonant
Write a Python program to test whether a letter is a vowel or a consonant.
Input
"e", "K", "3"
Output
e -> vowel K -> consonant 3 -> not a single letter
Approach
- Reject non-letters, then check membership in “aeiou”.
Solution
def kind(ch): if not ch.isalpha() or len(ch) != 1: return "not a single letter" return "vowel" if ch.lower() in "aeiou" else "consonant" for ch in ["e", "K", "3"]: print(ch, "->", kind(ch))Output
e -> vowel K -> consonant 3 -> not a single letter
Line by line
Lines 2-3
Digits, symbols and longer strings are rejected first.
Learn the concept
New to this topic? Read the lesson, then come back.