How to detect vowels vs consonants in Python?

PythonServer Side ProgrammingProgramming

First you should check if the character is an alphabet or not. Then you can create a list of vowels and check if the character is a vowel using this. If not then it must be a consonant. For example,

def vowel_or_consonant(c):
    if not c.isalpha():
        return 'Neither'
    vowels = 'aeiou'
    if c.lower() in vowels:
        return 'Vowel'
    else:
        return 'Consonant'
for c in "hello people":
    print c, vowel_or_consonant(c)

This will give the output:

h Consonant
e Vowel
l Consonant
l Consonant
o Vowel
  Neither
p Consonant
e Vowel
o Vowel
p Consonant
l Consonant
e Vowel
raja
Published on 19-Dec-2017 10:36:40
Advertisements