Editor

Python Program to Check if a Character Is a Vowel or Consonant

Beginner · Basics

The vowels of the English alphabet are a, e, i, o and u, and every other letter is a consonant. This program checks a single character and says whether it is a vowel, a consonant or not a letter at all.

Program

vowel_or_consonant.py
ch = "E" # or: ch = input("Enter a character: ")
if len(ch) != 1 or not ch.isalpha():
print(f"'{ch}' is not a single letter")
elif ch.lower() in "aeiou":
print(f"'{ch}' is a vowel")
else:
print(f"'{ch}' is a consonant")
Output
'E' is a vowel

How it works

  1. The first check makes sure the input is exactly one character and that it is a letter, so "7", "?" and "ab" are rejected.
  2. ch.lower() turns a capital letter into a small one, so "E" and "e" give the same answer.
  3. ch.lower() in "aeiou" is True when the letter is one of the five vowels, and every other letter is a consonant.
  4. The length check matters: an empty string "" counts as being inside every string, so without it an empty input would be called a vowel.

Other ways to write it

Using or between comparisons

Using or between comparisons
ch = "u"
ch = ch.lower()
if ch == "a" or ch == "e" or ch == "i" or ch == "o" or ch == "u":
print(ch, "is a vowel")
else:
print(ch, "is a consonant")
Output
u is a vowel

This is how many textbooks write it: each == compares with one vowel, and or makes the condition true if any of them matches. Writing ch == "a" or "e" would be a bug, because "e" on its own always counts as true.

Checking several characters

Checking several characters
def kind_of(ch):
if not ch.isalpha():
return "not a letter"
if ch.lower() in ["a", "e", "i", "o", "u"]:
return "a vowel"
return "a consonant"
for ch in "PyThOn 3!":
print(f"'{ch}' is {kind_of(ch)}")
Output
'P' is a consonant
'y' is a consonant
'T' is a consonant
'h' is a consonant
'O' is a vowel
'n' is a consonant
' ' is not a letter
'3' is not a letter
'!' is not a letter

Checking against a list of single letters, rather than the string "aeiou", only ever matches one whole letter, and the function makes it easy to test every character of a string.

Try it yourself

Change the program so it also says whether the letter is a capital or a small letter.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises