Check if Character is Vowel or Consonant
Separate a vowel, consonant, or nonletter with string tests. This beginner lesson covers lowercasing, membership, and the single-character input assumption.
1# Take a character as input2ch = input("Enter a character: ").lower()34# Check if vowel or consonant5if ch and ch in 'aeiou':6 print("Vowel")7elif ch.isalpha():8 print("Consonant")9else:10 print("Not an alphabet")
Output
Enter a character: a Vowel Enter a character: b Consonant Enter a character: 5 Not an alphabet
In this one
- Normalize English letter case
- Use string membership
- Guard empty text
How it works
- 1
Read ch and lowercase it.
- 2
Require nonempty text before vowel membership.
- 3
Otherwise test alphabetic text or print nonletter feedback.
Edge cases
What happens for A?
What happens for A?
lower produces a, so the result is Vowel.
Why guard an empty response?
Why guard an empty response?
The empty string is a substring of every string; ch must be nonempty before vowel membership is accepted.
What's going on
The intended input is one English letter. lower turns A into a before the program checks membership in the vowel string. That makes the vowel test case-insensitive.
If the vowel test fails, isalpha asks whether the response contains only letters. A letter then becomes Consonant; digits and punctuation reach the last branch. Membership in a string checks substrings, so validating a single character matters before using this as a general text checker.
What happens for A? lower produces a, so the result is Vowel.
Why guard an empty response? The empty string is a substring of every string; ch must be nonempty before vowel membership is accepted.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does lower change?
Pick an answer to check it.
Question 2
What happens for A?
Pick an answer to check it.
Questions people ask
What happens for A?
lower produces a, so the result is Vowel.
Why guard an empty response?
The empty string is a substring of every string; ch must be nonempty before vowel membership is accepted.
What does lower change?
Letter case.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum