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
Python
defkind(ch):ifnotch.isalpha()orlen(ch)!=1:return"not a single letter"return"vowel"ifch.lower()in"aeiou"else"consonant"forchin["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.
Write a Python program to build a string from the first 2 and the last 2 characters of a string. If the string is shorter than 2 characters, return an empty string.