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
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.
Enter a letter: E E is a vowel
How it works
.strip()removes spaces that the user typed before or after the letter.letter.isalpha()isTrueonly for letters, so "7" and "?" get the message instead of being called consonants.letter.lower()turns "E" into "e", so capital letters are checked against the same"aeiou".- The
len(letter) != 1check is needed becauseinlooks for a substring."ae" in "aeiou"isTrue, so without the length check, typing "ae" would print that it is a vowel.
Checking many inputs
The same rules in a function, tested on several inputs at once:
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))
'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.