A Python program to check whether a character is a vowel or consonant determines whether the given alphabet belongs to the vowel group (A, E, I, O, U) or the consonant group. In Python, we can perform this check using if-else conditional statements and the in operator.
Problem statement
Write a Python program to check whether the given character is a vowel or consonant.
The program should:
- Accept a single character from the user.
- Check whether the entered character is an alphabet.
- Identify whether it belongs to the vowels (a, e, i, o, u).
- Display whether the character is a vowel or consonant.
- Display an appropriate message if the input is invalid.
For example:
| Input | Output |
|---|---|
| a | Vowel |
| B | Consonant |
| E | Vowel |
| z | Consonant |
| 5 | Invalid input |
Algorithm to Check Vowel or Consonant in Python
- Start the program.
- Read a character from the user.
- Check whether the input contains exactly one alphabet.
- If the input is invalid, display an error message.
- Otherwise, convert the character into lowercase.
- Check whether the character belongs to the vowel group (a, e, i, o, u).
- If the condition is true, display “Vowel”.
- Otherwise, display “Consonant”.
- Stop the program.
Python code to check given character is vowel or consonant
ch = input("Enter a character: ")
if len(ch) == 1 and ch.isalpha():
if ch.lower() in "aeiou":
print(ch, "is a Vowel")
else:
print(ch, "is a Consonant")
else:
print("Please enter a single alphabet character.")
Output 1:
Enter a character: a
a is a Vowel
Output 2:
Enter a character: B
B is a Consonant
Output 3:
Enter a character: 5
Please enter a single alphabet character.
Explanation of the Python Program
The program works by checking whether the entered input is a single alphabet and then determining whether it is a vowel or consonant.
Step-by-step logic:
- Accept input: Use the
input()function to get a character from the user and store it in the variablech. - Validate the input: Use
len(ch) == 1to check whether the input contains exactly one character. - Check alphabet: Use the
isalpha()method to ensure that the entered character is an alphabet. - Convert to lowercase: Use
lower()to convert the character into lowercase so that both uppercase and lowercase vowels can be checked. - Check vowel: Use the
inoperator to check whether the character exists in the string"aeiou". - Display the result: If the character belongs to the vowel group, print “Vowel”. Otherwise, print “Consonant”.
- Handle invalid input: If the input is not a single alphabet character, display an appropriate error message.
How Does the Python Vowel Checking Logic Work?
The main condition used in this program is:
if ch.lower() in "aeiou":
Here is how it works:
ch.lower()converts the entered character into lowercase."aeiou"contains all five English vowels.- The
inoperator checks whether the character exists in the given string. - If the character is found, the condition returns
True. - Otherwise, it returns
False.
Example:
If the input is E, the lower() method converts it into e. Since e exists in "aeiou", the condition becomes true, and the program prints E is a Vowel.
Time and Space Complexity
| Complexity | Explanation |
|---|---|
| Time Complexity | O(1) |
| Space Complexity | O(1) |