Vowel or Consonant

Check whether an alphabetic character is a vowel or a consonant.

PythonBeginner

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:

  1. Validate that the input is exactly one alphabetic character.
  2. Convert it to lowercase with .lower() to handle both upper and lower case.
  3. Check membership in the string 'aeiou' to decide if it is a vowel.
  4. Otherwise, it must be a consonant.

Step-by-Step Breakdown

  1. 1Read a character and convert it to lowercase.
  2. 2Ensure it is exactly one alphabetic character.
  3. 3If it is in the vowel set, print vowel.
  4. 4Otherwise, print consonant.