How to Find Vowels and Consonants in Python

In this post, we will discuss how to find vowels and consonants in python using the for loop. The alphabets ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (in uppercase) and ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels, and remaining alphabets are called consonants.

Python Program to Find Vowels and Consonants

This python program uses the if-else statement to find character vowels or consonants. We will take a character while declaring the variables. Then, find whether a character is a vowel or consonant. Finally, the result will be displayed on the screen.

# Python program to find character is vowel or consonant

# take input
ch = input('Enter any character: ')

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

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

Enter any character: a
a is a Vowel

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

Enter any character: K
K is a Consonant

How to Find Vowels and Consonants in Python

We will take a string while declaring the variables. Find vowels and consonants from the string using the For Loop, len() function and list comprehension. Finally, the vowels and consonants will be displayed on the screen.

# Python program to find vowels and consonants in a string

# take input
string = input('Enter any string: ')

# to find the vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
# to find the consonants
consonants = [each for each in string if each not in "aeiouAEIOU "]

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

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

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

Enter any 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:-

Enter any 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

We can also take the help of a function to find vowels and consonants in a string in python. A function is a block of code that performs a specific task.

# Python program to find vowels and consonants in a string

def findVowels(string):  #user-defined function
   # to find the vowels
   vowels = [each for each in string if each in "aeiouAEIOU"]
   print('Number of vowels in string:', len(vowels))
   print(vowels)

def findConsonants(string):  #user-defined function
   # to find the consonants
   consonants = [each for each in string if each not in "aeiouAEIOU "]
   print('Number of consonant in string:', len(consonants))
   print(consonants)

# take input
string = input('Enter any string: ')

# calling function
findVowels(string)
findConsonants(string)

Output:-

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

Python Program to Find Vowels and Consonants in a String

In this program, we use the casefold() method to ignore the cases. The casefold() method returns a string where all the characters are lower case. Also, we use the .fromkeys() method. The fromkeys() method creates a new dictionary from the given sequence of … ‘i’, ‘o’, ‘u’ } value = [1].

# Python program to find vowels and consonants in a string

def findVowels(string, vowels):
    
   # using dictionary
   count = {}.fromkeys(vowels, 0)
   string = string.casefold()
    
   # to find the vowels
   for char in string:
      if char in count:
        count[char] += 1
   return count

def findConsonants(string, consonants):
    
   # using dictionary
   count = {}.fromkeys(consonants, 0)
   string = string.casefold()
    
   # to find the consonants
   for char in string:
      if char in count:
        count[char] += 1
   return count

# take input
string = input('Enter any string: ')

vowels = 'aeiou'
consonants = 'bcdfghjklmnpqrstvwxyz '

# calling function
print(findVowels(string, vowels))
print(findConsonants(string, consonants))

Output:-

Enter any 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}

Also See:- Check if string starts with vowel in Python

Leave a Comment

Your email address will not be published. Required fields are marked *