In this article, we are going to write a Python program to count and display consonants from a string with the help of various examples. Here we will go through some custom methods we well as use some built-in methods to count and display consonants from the string.
Let’s see all the ways to count and display consonants from string one by one.
Headings of Contents
Python Program to Count and Display Consonants from a String
There are various ways to display consonants from the string that are described below along with proper examples.
- Using list comprehension
- Using for loop
- Using set comprehension
- Using Dictionary ways
- Using fromkeys() method
Using list comprehension
Here we will use list comprehension to count and display consonants from a string.
Example: Python program to count and display consonants from a string
string = "python is good language"
vowels = 'aeiou'
result = [v for v in string if v not in vowels]
print(''.join(result))
print("Total consonants are: ", len(result))
Output
pythn s gd lngg
Total consonants are: 15
Using for loop
Python for loop is another best way to display consonants from a string.
Example: Python program to count and display consonants from a string
string = "python is good language"
vowels = 'aeiou'
result = ''
for i in string:
if i not in vowels:
result += i
print("The Result is:- ", result)
print("Total consonants are:- ", len(result))
Output
The result is:- pythn s gd lngg
Total consonants are:- 15
Python for loop:- Click Here
Using set comprehension
If you want to display only distinct consonants then you can use set comprehension.set comprehension always starts with a curly bracket { }
.
Example: Python program to display consonants from a string
string = "python is good language"
vowels = 'aeiou'
result = {v for v in string if v not in vowels}
print(''.join(result))
Output is:- ygdhlptns
Using Dictionary ways
Dictionary is the best way to count and display consonants from the string. Let’s see how can I do that.
Example: Python program to count and display consonants from a string
temp = {}
string = "python is good language"
vowels = 'aeiou'
for i in string:
if i not in vowels:
if temp.get(i):
temp[i] = temp[i] + 1
else:
temp.update({i: 1})
print(temp)
Output
{'p': 1, 'y': 1, 't': 1, 'h': 1, 'n': 2, ' ': 3, 's': 1, 'g': 3, 'd': 1, 'l': 1}
Using fromkeys() dictionary method
Use the dictionary fromkeys()
method to count and display consonants from a string.
Example: Python program to count consonants from a string
string = "python is good language"
const = 'bcdfghjklmnpqrstvwxyz'
temp_dict = dict.fromkeys(const, 0)
for i in string:
if i in const:
temp_dict[i] += 1
print(temp_dict)
Output
{'b': 0, 'c': 0, 'd': 1, 'f': 0, 'g': 3, 'h': 1, 'j': 0, 'k': 0, 'l': 1, 'm': 0, 'n': 2, 'p': 1, 'q': 0, 'r': 0, 's': 1, 't': 1, 'v': 0, 'w': 0, 'x': 0, 'y': 1, 'z': 0}
Dictionary fromkeys()
Reference:- Click Here
Conclusion
So, here we have seen everything about how to count and display consonants from string with the help of various examples.
These all are the best ways to count and display consonants from the string. This question might be asked by the interviewer when you are going for a junior Python developer position.
I hope this article will help you. If you like this article, please share, support, and keep visiting for further tutorials.
Related Articles:-
Thanks for your valuable time … 🙏🙏🙏