Vowel or Consonant

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

BeginnerTopic: Conditional Programs
Back

What You'll Learn

  • Using string methods like lower() and isalpha()
  • Checking membership with in
  • Combining validation with core logic

Python Vowel or Consonant Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# 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

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.

Understanding Vowel or Consonant

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.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.