How to Find Vowels and Consonants in Python

How to Find Vowels and Consonants in Python | we use the if-elif-else statement and for loop to find vowel and consonant.

Python Program to Find Vowels and Consonants

This python program uses the if-elif-else statements to find character vowels or consonants.

# Python code to find character is vowels or consonants

# inputs from the user
c = input('Enter a Characters: ')

# find vowel or constant and display result
if(c=='A' or c=='a' or c=='E' or c =='e' 
            or c=='I' or c=='i' or c=='O' 
              or c=='o' or c=='U' or c=='u'):
    print(c, "is a Vowels.")
else:
    print(c, "is a Consonants.")

Output for the input values test-case-1:-

Enter a characters: a
a is a Vowels.

Output for the input values test-case-2:-

Enter a characters: K
K is a Consonants.

How to Find Vowels and Consonants in Python

Find vowels and consonants from a string using the For Loop, len() function, and list comprehension.

# Python code to find vowel and consonant in a string

# inputs from the user
string = input('String: ')

# vowel letters
vowel = [ch for ch in string if ch in 'AEIOUaeiou']
# consonant letters
consonant = [ch for ch in string if ch not in 'AEIOUaeiou']

# print number of vowels in string
print('Number of vowel in string:', len(vowel))
# print all vowels in string
print(vowel)

# print number of consonants in string
print('Number of consonant in string:', len(consonant))
# print all consonant in string
print(consonant)

Output for the input values test-case-1:-

String: Know Program
Number of vowel in string: 3
[‘o’, ‘o’, ‘a’]
Number of consonant in string: 8
[‘K’, ‘n’, ‘w’, ‘P’, ‘r’, ‘g’, ‘r’, ‘m’]

Output for the input values test-case-2:-

String: Vowels Consonants
Number of vowel in string: 5
[‘o’, ‘e’, ‘o’, ‘o’, ‘a’]
Number of consonant in string: 11
[‘V’, ‘w’, ‘l’, ‘s’, ‘C’, ‘n’, ‘s’, ‘n’, ‘n’, ‘t’, ‘s’]

How to Find Vowels and Consonants in String in Python

# Python code to find vowel and consonant in a string

def findVowels(string):  #user-defined function
   # vowel letters
   vowel = [ch for ch in string if ch in 'AEIOUaeiou']
   print('Number of vowels in the string:', len(vowel))
   print(vowel)

def findConsonants(string):  #user-defined function
   # consonant letters
   consonant = [ch for ch in string if ch not in 'AEIOUaeiou']
   print('Number of consonant in the string:', len(consonant))
   print(consonant)

# inputs from the user
string = input('String: ')

# functions calling
findVowels(string)
findConsonants(string)

Output:-

String: Programming Language
Number of vowels in the string: 7
[‘o’, ‘a’, ‘i’, ‘a’, ‘u’, ‘a’, ‘e’]
Number of consonant in the string: 12
[‘P’, ‘r’, ‘g’, ‘r’, ‘m’, ‘m’, ‘n’, ‘g’, ‘L’, ‘n’, ‘g’, ‘g’]

Python Program to Find Vowels and Consonants in a String

we have using the casefold() function and fromkeys() function.

# Python code to find vowel and consonant in string

def findVowel(string, vowel):
    
   # dictionary
   count = {}.fromkeys(vowel, 0)
   string = string.casefold()
    
   # vowel letters
   for ch in string:
      if ch in count:
        count[ch] += 1
   return count

def findConsonant(string, consonant):
    
   # using dictionary
   count = {}.fromkeys(consonant, 0)
   string = string.casefold()
    
   # consonants letters
   for ch in string:
      if ch in count:
        count[ch] += 1
   return count

# input from the user
string = input('String: ')

vowel = 'aeiou'
consonant = 'bcdfghjklmnpqrstvwxyz '

# calling function
print(findVowel(string, vowel))
print(findConsonant(string, consonant))

Output:-

String: Python program to find vowels and consonants
Number of vowels: {‘a’: 3, ‘e’: 1, ‘i’: 1, ‘o’: 6, ‘u’: 0}
Number of consonants: {‘b’: 0, ‘c’: 1, ‘d’: 2, ‘f’: 1, ‘g’: 1, ‘h’: 1, ‘j’: 0, ‘k’: 0, ‘l’: 1, ‘m’: 1, ‘n’: 6, ‘p’: 2, ‘q’: 0, ‘r’: 2, ‘s’: 3, ‘t’: 3, ‘v’: 1, ‘w’: 1, ‘x’: 0, ‘y’: 1, ‘z’: 0, ‘ ‘: 6}

Leave a Comment

Your email address will not be published.