Python: Find the string consisting of all the words whose lengths are prime numbers
Python Programming Puzzles: Exercise-64 with Solution
Write a Python program to find the string consisting of all the words whose lengths are prime numbers.
Input: The quick brown fox jumps over the lazy dog. Output: The quick brown fox jumps the Input: Omicron Effect: Foreign Flights Won't Resume On Dec 15, Decision Later. Output: Omicron Effect: Foreign Flights Won't On Dec 15,
Pictorial Presentation:

Sample Solution:
Python Code:
#License: https://bit.ly/3oLErEI
def test(strs):
return " ".join(strs for strs in strs.split() if is_prime(len(strs)))
def is_prime(n):
return n > 1 and all(n % j for j in range(2, int(n ** 0.5) + 1))
strs = "The quick brown fox jumps over the lazy dog."
print("Original list of numbers:")
print(strs)
print("Words whose lengths are prime numbers in the said string:")
print(test(strs))
strs = "Omicron Effect: Foreign Flights Won't Resume On Dec 15, Decision Later."
print("\nOriginal list of numbers:")
print(strs)
print("Words whose lengths are prime numbers in the said string:")
print(test(strs))
Sample Output:
Original list of numbers: The quick brown fox jumps over the lazy dog. Words whose lengths are prime numbers in the said string: The quick brown fox jumps the Original list of numbers: Omicron Effect: Foreign Flights Won't Resume On Dec 15, Decision Later. Words whose lengths are prime numbers in the said string: Omicron Effect: Foreign Flights Won't On Dec 15,
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the sum of the even elements that are at odd indices in a given list.
Next: Circular shift number.
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