w3resource

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']

Visual Presentation:

Python: Find all words in a given string with n consonants.

Sample Solution-1:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a string 'strs' and an integer 'n' as inputs
def test(strs, n):
    # List comprehension to filter words from the input string based on the number of consonants
    return [w for w in strs.split() if sum([c not in "aeiou" for c in w.lower()]) == n]

# Assign a specific string 'strs' to the variable
strs = "this is our time"
# Print a message indicating the original string
print("Original string:", strs)
# Assign a specific value 'n' to the variable
n = 3
# Print a message indicating the number of consonants to be checked
print("Number of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable
n = 2
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable
n = 1
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
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:

Flowchart: Python - Find all words in a given string with n consonants.

Sample Solution-2:

Python Code:

# Define a function named 'test' that takes a string 'strs' and an integer 'n' as inputs
def test(strs, n):
    # List comprehension to filter words from the input string based on the number of consonants
    return [w for w in strs.split() if sum(c.lower() not in "aeiou" for c in w) == n]

# Assign a specific string 'strs' to the variable
strs = "this is our time"
# Print a message indicating the original string
print("Original string:", strs)
# Assign a specific value 'n' to the variable
n = 3
# Print a message indicating the number of consonants to be checked
print("Number of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable
n = 2
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable
n = 1
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
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:

Flowchart: Python - Find all words in a given string with n consonants.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/puzzles/python-programming-puzzles-43.php