PythonMastery
Python programs · Strings · beginner

Check if a letter is a vowel or consonant in Python

Read one letter and print whether it is a vowel or a consonant. Capital letters work, and digits, symbols and longer text get a clear message.

The English vowels are a, e, i, o and u. Every other letter is a consonant. The program first checks that the input really is one letter, then checks whether that letter is in "aeiou".

The program

python
letter = input("Enter a letter: ").strip()

if len(letter) != 1 or not letter.isalpha():
    print("Please enter exactly one letter.")
elif letter.lower() in "aeiou":
    print(f"{letter} is a vowel")
else:
    print(f"{letter} is a consonant")

Typed in this example: E. Press Run to type your own.

output
Enter a letter: E
E is a vowel

How it works

Checking many inputs

The same rules in a function, tested on several inputs at once:

python
def classify(text):
    text = text.strip()
    if len(text) != 1 or not text.isalpha():
        return "not a single letter"
    if text.lower() in "aeiou":
        return "vowel"
    return "consonant"

for t in ["a", "Q", "U", "y", "7", "?", "ae", ""]:
    print(repr(t), classify(t))
output
'a' vowel
'Q' consonant
'U' vowel
'y' consonant
'7' not a single letter
'?' not a single letter
'ae' not a single letter
'' not a single letter

repr(t) prints the text with quotes, so you can see the empty string ''. The letter "y" is sometimes used as a vowel in English ("gym"), but most programs treat it as a consonant, as this one does. isalpha() also accepts letters from other alphabets, such as "é" and "ß". If you want only the 26 English letters, check letter in string.ascii_letters (after import string) instead.

Learn the idea: Working with Strings, Control Flow: Conditionals