Vowel or Consonant in Python
ch in 'aeiou'. lower() first or 'A' looks like a consonant.
BeginnerConditional ProgramsExample 2 of 20
vowel-or-consonant.py
1# Program to check whether a character is a vowel or consonant23ch = input("Enter a single alphabet character: ").lower()45if len(ch) != 1 or not ch.isalpha():6 print("Please enter exactly one alphabetic character.")7else:8 if ch in 'aeiou':9 print(ch, "is a vowel")10 else:11 print(ch, "is a consonant")
Output
Enter a single alphabet character: a a is a vowel
In this one
- lower() before the vowel check
- Y is a consonant in this program. Live with it.
What's going on
ch in 'aeiou'. That's the vowel test. 'A' is not in that string, so .lower() first or every capital is a consonant.
isalpha() plus len == 1 because "ae" and "3" are not the question you asked.
Y is a consonant here. Linguists can fight it in the comments.
After this program
If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.