What You'll Learn
- •Using string methods like lower() and isalpha()
- •Checking membership with in
- •Combining validation with core logic
Python
# Program to check whether a character is a vowel or consonant
ch = input("Enter a single alphabet character: ").lower()
if len(ch) != 1 or not ch.isalpha():
print("Please enter exactly one alphabetic character.")
else:
if ch in 'aeiou':
print(ch, "is a vowel")
else:
print(ch, "is a consonant")Output
Enter a single alphabet character: a a is a vowel
We:
- Validate that the input is exactly one alphabetic character.
- Convert it to lowercase with
.lower()to handle both upper and lower case. - Check membership in the string
'aeiou'to decide if it is a vowel. - Otherwise, it must be a consonant.
Step-by-Step Breakdown
- 1Read a character and convert it to lowercase.
- 2Ensure it is exactly one alphabetic character.
- 3If it is in the vowel set, print vowel.
- 4Otherwise, print consonant.