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.
def toString(s):
str1 = ""
for ele in s:
str1 += ele
return str1
def remove(sentence, n):
n_words = []
words = sentence.split(" ")
for word in words:
if not len(word) == n:
n_words.append(word)
n_words.append(" ")
return toString(n_words)
a = str(input("Enter a sentence:"))
n = int(input("Enter n (len to remove):"))
print(remove(a, n))
Comments
Leave a comment