Given a string, write a program to remove all the words with K length.
Input
The first line of input String A.
The second line of input 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.
Use the following set of codes:
# A program to remove all words with k length from a string
string = input("Please enter a string: ")
k = int(input("Enter integer k: "))
result = string.split()
list_0 = []
while result:
current = result.pop()
if len(current) != k:
list_0.append(current)
new_list = reversed(list_0)
string = ' '.join([str(item) for item in new_list])
print(string)
Comments
Leave a comment