Answer to Question #178019 in Python for adhi chinna

Question #178019

Remove Words

Given a string, write a program to remove all the words with K length.Input


The first line of the input will contain a string A.

The second line of the input will contain an integer K.Output


The output should contain a string after removing all the words whose length is equal to K.Explanation


For example, string A is "Tea is good for you", k is 3 then output should be "is good."

Here words "Tea", "for", "you" length is equal to 3, so these words are removed from string.

Sample Input 1

Tea is good for you

3

Sample Output 1

is good

Sample Input 2

A gang stood in front of me

2

Sample Output 2

A gang stood front



1
Expert's answer
2021-04-05T04:22:02-0400
import re
# get the input string from the user
inputString =input("");
# get K from the user
K =int(input(""));
words=re.findall(r'\w+',inputString)
for w in words:
    if len(w)==K:
        inputString=inputString.replace(w,"").replace("  "," ")
        inputString=inputString.replace(" .",". ")
        inputString=inputString.replace(" !","! ")
        inputString=inputString.replace(" ?","? ")
        
# Display a new string.
print(inputString.strip())

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS