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
'E' is a vowel
How it works
- The first check makes sure the input is exactly one character and that it is a letter, so "7", "?" and "ab" are rejected.
ch.lower()turns a capital letter into a small one, so "E" and "e" give the same answer.ch.lower() in "aeiou"isTruewhen the letter is one of the five vowels, and every other letter is a consonant.- 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
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
'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.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises