Python: Find all words in a given string with n consonants
Python Programming Puzzles: Exercise-43 with Solution
From vocabulary.com:
A consonant is a speech sound that is not a vowel. It also refers to letters of the alphabet that represent those sounds: Z, B, T, G, and H are all consonants. Consonants are all the non-vowel sounds, or their corresponding letters: A, E, I, O, U and sometimes Y are not consonants.
Write a Python program to find all words in a given string with n consonants.
Input: this is our time Output: Number of consonants: 3 Words in the said string with 3 consonants: ['this'] Number of consonants: 2 Words in the said string with 2 consonants: ['time'] Number of consonants: 1 Words in the said string with 1 consonants: ['is', 'our']
Pictorial Presentation:

Sample Solution-1:
Python Code:
#License: https://bit.ly/3oLErEI
def test(strs, n):
return [w for w in strs.split() if sum([c not in "aeiou" for c in w.lower()]) == n]
strs = "this is our time"
print("Original string:",strs)
n = 3
print("Number of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 2
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 1
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
Sample Output:
Original string: this is our time Number of consonants: 3 Words in the said string with 3 consonants: ['this'] Number of consonants: 2 Words in the said string with 2 consonants: ['time'] Number of consonants: 1 Words in the said string with 1 consonants: ['is', 'our']
Flowchart:

Sample Solution-2:
Python Code:
def test(strs, n):
return [w for w in strs.split() if sum(c.lower() not in "aeiou" for c in w) == n]
strs = "this is our time"
print("Original string:",strs)
n = 3
print("Number of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 2
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 1
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
Sample Output:
Original string: this is our time Number of consonants: 3 Words in the said string with 3 consonants: ['this'] Number of consonants: 2 Words in the said string with 2 consonants: ['time'] Number of consonants: 1 Words in the said string with 1 consonants: ['is', 'our']
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the set of distinct characters in a string, ignoring case.
Next: Determine which characters of a hexadecimal number correspond to prime numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join