How to Find Consonants in a String in Python

Previously we had to develop a Python program to check vowels or consonants. In this post, we will discuss how to find consonants in a string 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 Consonants in a String

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

# Python program to find consonants in a string

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

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

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

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

Enter any string: Know Program
Number of consonants in string: 8
[‘K’, ‘n’, ‘w’, ‘P’, ‘r’, ‘g’, ‘r’, ‘m’]

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

Enter any string: Consonant
Number of consonants in string: 6
[‘C’, ‘n’, ‘s’, ‘n’, ‘n’, ‘t’]

Find Consonants in String in Python

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

# Python program to find consonants in a string

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

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

# calling function
findConsonants(string)

Output:-

Enter any string: Learn Consonant
Number of consonants in string: 9
[‘L’, ‘r’, ‘n’, ‘C’, ‘n’, ‘s’, ‘n’, ‘n’, ‘t’]

Program to Find Consonants in a String in Python

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 consonants in a string

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

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

# calling function
consonants = 'bcdfghjklmnpqrstvwxyz'
print(findConsonants(string, consonants))

Output:-

Enter any string: Find consonants in string in python
{‘b’: 0, ‘c’: 1, ‘d’: 1, ‘f’: 1, ‘g’: 1, ‘h’: 1, ‘j’: 0, ‘k’: 0, ‘l’: 0, ‘m’: 0, ‘n’: 8, ‘p’: 1, ‘q’: 0, ‘r’: 1, ‘s’: 3, ‘t’: 3, ‘v’: 0, ‘w’: 0, ‘x’: 0, ‘y’: 1, ‘z’: 0}

Leave a Comment

Your email address will not be published.