Python program to check whether input character is vowel or consonant using if else
In this article, you will learn how to make a Python program to check whether the input character is vowel or consonant using if-else in the Python programming language.
Examples
Enter a character to check: U
' U ' is a vowel character.
Enter a character to check: 7
Sorry, You entered a Non-Alphabetic character!
You should have knowledge of the following topics in Python programming to understand this program:
- Python String functions
- Python
if-else
condition statement - Python
input()
function - Python
print()
function
Vowel Characters
Consonant Characters
Source Code
# Python program to check whether input character is vowel or consonant using if-else
print("Enter a character to check: ", end="")
random_character, lowercase, uppercase = str(input()), 0, 0
# isalpha() method will check
# whether character is alphabetic or non-alphabetic
if (random_character and random_character.isalpha()):
# it will be return 1
# if input character matched with these small characters
lowercase = random_character == 'a' or random_character == 'e' or random_character == 'i' or random_character == 'o' or random_character == 'u'
# it will be return 1
# if input character matched with these capital characters
uppercase = random_character == 'A' or random_character == 'E' or random_character == 'I' or random_character == 'O' or random_character == 'U'
if (lowercase or uppercase):
print("\n'", random_character, "' is a vowel character.")
else:
print("\n'", random_character, "' is a consonant character.")
else:
print("\nSorry, You entered a Non-Alphabetic character!")
Output
Enter a character to check: U
' U ' is a vowel character.
Explanation
In this program, we have taken input U
from the user and checked for its Alphabetic
or Non-Alphabetic
character.
Then we matched this character with a series of vowel characters, If matched with the vowel characters series then it's vowel character.
If didn't match with the vowel characters series then It's a consonant character.