w3resource

Python: Cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters

Python Basic - 1: Exercise-60 with Solution

Internet search engine giant, such as Google accepts web pages around the world and classify them, creating a huge database. The search engines also analyze the search keywords entered by the user and create inquiries for database search. In both cases, complicated processing is carried out in order to realize efficient retrieval, but basics are all cutting out words from sentences.

Write a Python program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

Input:
English sentences consisting of delimiters and alphanumeric characters are given on one line.
Input a sentence (1024 characters. max.)
The quick brown fox
3 to 6 characters length of words:
The quick brown fox

Sample Solution:

Python Code:

# Print statement to prompt the user to input a sentence (1024 characters max.)
print("Input a sentence (1024 characters max.)")

# Input a sentence and store it in the variable 'yy'
yy = input()

# Replace commas and periods with spaces in the input sentence
yy = yy.replace(",", " ")
yy = yy.replace(".", " ")

# Print statement to display words with lengths between 3 and 6 characters
print("3 to 6 characters length of words:")

# Print the words with lengths between 3 and 6 characters using list comprehension
print(*[y for y in yy.split() if 3 <= len(y) <= 6])

Sample Output:

Input a sentence (1024 characters. max.)
 The quick brown fox
3 to 6 characters length of words:
The quick brown fox

Explanation:

Here is a breakdown of the above Python code:

  • First print a statement to prompt the user to input a sentence.
  • Input a sentence and store it in the variable 'yy'.
  • Replace commas and periods with spaces in the input sentence.
  • Print a statement to indicate the words with lengths between 3 and 6 characters.
  • Use list comprehension to filter words with lengths between 3 and 6 characters.
  • Print the filtered words separated by spaces.

Visual Presentation:

Python: Cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.
Python: Cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

Flowchart:

Flowchart: Python - Cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections.
Next: Write a Python program that compute the maximum value of the sum of the passing integers according to the specified rules.

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://w3resource.com/python-exercises/basic/python-basic-1-exercise-60.php