Vowel or Consonant in Python
Normalize A to a before testing Python vowel membership. A beginner character exercise combines validation with an honest look at its English-only vowel rule.
1# Program to check whether a character is a vowel or consonant23ch = input("Enter a single alphabet character: ").lower()45if len(ch) != 1 or not ch.isalpha():6 print("Please enter exactly one alphabetic character.")7else:8 if ch in 'aeiou':9 print(ch, "is a vowel")10 else:11 print(ch, "is a consonant")
Output
a is a vowel
The input a is present in the five-letter vowel string.
In this one
- Normalize case before membership tests
- Combine length and letter checks
- State the language limits of a classifier
How it works
- 1
Read input and save the result of lower().
- 2
Reject responses that are not exactly one alphabetic character.
- 3
Check membership in aeiou and print the selected category.
Edge cases
Capital vowel
Enter A.
lower changes it to a, which passes the vowel test.
Two letters
Enter ae.
The length check rejects the response instead of classifying the pair.
What's going on
The vowel test is ch in 'aeiou'. Uppercase A is not in that lowercase string, so lower() runs first. It returns a lowercase string; it does not edit input text in place.
len(ch) != 1 or not ch.isalpha() rejects empty responses, several letters, digits, and punctuation. Only then does the nested condition choose vowel or consonant. Y belongs to the consonant branch in this simple rule.
isalpha recognizes Unicode letters, not just English ones. A letter such as é passes validation but is not in the five-character vowel list, so this program calls it a consonant. That is a limitation of the English rule, not a claim about every language.
After this program
If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why call lower before the vowel test?
Pick an answer to check it.
Question 2
Does isalpha mean English letters only?
Pick an answer to check it.
Questions people ask
Why call lower before the vowel test?
The vowel list is lowercase, so normalization lets uppercase English vowels match.
Does isalpha mean English letters only?
No. It recognizes Unicode letters, while this vowel list covers only aeiou.
Is y treated as a vowel here?
No. Only the five letters in aeiou take the vowel branch.