Vowel or consonant

EasyString Programs · 6 of 106

Write a Python program to test whether a letter is a vowel or a consonant.

Advertisement
Input
"e", "K", "3"
Output
e -> vowel
K -> consonant
3 -> not a single letter

Approach

  1. Reject non-letters, then check membership in “aeiou”.

Solution

Python
def kind(ch):    if not ch.isalpha() or len(ch) != 1:        return "not a single letter"    return "vowel" if ch.lower() in "aeiou" else "consonant" for ch in ["e", "K", "3"]:    print(ch, "->", kind(ch))
Output
e -> vowel
K -> consonant
3 -> not a single letter

Line by line

Lines 2-3

Digits, symbols and longer strings are rejected first.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Leave a Comment