Python Program to Check if Character is Vowel or Consonant
Use a Python program that takes a character input and checks if it is in
'aeiouAEIOU' to determine if it's a vowel; otherwise, it is a consonant, like if char.lower() in 'aeiou': print('Vowel') else: print('Consonant').Examples
Inputa
OutputVowel
InputB
OutputConsonant
Inputz
OutputConsonant
How to Think About It
To check if a character is a vowel or consonant, first consider vowels as the letters a, e, i, o, u in both uppercase and lowercase. Then, compare the input character to this set. If it matches any vowel, it is a vowel; otherwise, it is a consonant.
Algorithm
1
Get a single character input from the user.2
Convert the character to lowercase to handle uppercase inputs.3
Check if the character is in the set of vowels 'aeiou'.4
If yes, print 'Vowel'.5
Otherwise, print 'Consonant'.Code
python
char = input('Enter a single character: ') if char.lower() in 'aeiou': print('Vowel') else: print('Consonant')
Output
Enter a single character: a
Vowel
Dry Run
Let's trace the input 'B' through the code
1
Input character
User inputs 'B'
2
Convert to lowercase
'B'.lower() becomes 'b'
3
Check if vowel
'b' in 'aeiou' is False
4
Print result
Print 'Consonant'
| Step | Character | Lowercase | Is Vowel? | Output |
|---|---|---|---|---|
| 1 | B | b | No | Consonant |
Why This Works
Step 1: Convert to lowercase
Using char.lower() ensures the program treats uppercase and lowercase letters the same.
Step 2: Check membership
Checking if the character is in 'aeiou' quickly tells if it is a vowel.
Step 3: Print result
If the character is a vowel, print 'Vowel'; otherwise, print 'Consonant'.
Alternative Approaches
Using a set for vowels
python
char = input('Enter a character: ') vowels = {'a', 'e', 'i', 'o', 'u'} if char.lower() in vowels: print('Vowel') else: print('Consonant')
Using a set can be faster for membership checks and is more readable.
Using if-elif for each vowel
python
char = input('Enter a character: ').lower() if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u': print('Vowel') else: print('Consonant')
This method is longer and less efficient but explicit for beginners.
Complexity: O(1) time, O(1) space
Time Complexity
Checking if a character is in a fixed set of vowels takes constant time, O(1), because the set size is small and fixed.
Space Complexity
The program uses constant space, O(1), as it only stores a few variables and a small set of vowels.
Which Approach is Fastest?
Using a set for vowels is generally faster for membership checks than a string, but for such a small fixed set, the difference is negligible.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String membership check | O(1) | O(1) | Simple and readable code |
| Set membership check | O(1) | O(1) | Slightly faster membership, good for larger sets |
| If-elif chain | O(1) | O(1) | Explicit but verbose, good for beginners |
Always convert the input character to lowercase to simplify vowel checking.
Forgetting to handle uppercase letters causes wrong results for vowels like 'A' or 'E'.