Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python program to remove K length words in String

  • Last Updated : 24 Dec, 2021

Given a String, write a Python program to remove all the words with K length. 

Examples:

Input : test_str = ‘Gfg is best for all geeks’, K = 3 
Output : is best geeks 
Explanation : Gfg, for and all are of length 3, hence removed.

Input : test_str = ‘Gfg is best for all geeks’, K = 2 
Output : Gfg best for all geeks 
Explanation : is of length 2, hence removed. 

Method #1 : Using split() + join() + list comprehension + len()

In this each word is split using split(), and then lengths are checked using len(), and then are omitted matching K. At last words are joined.

Python3




# Python3 code to demonstrate working of
# Remove K length words in String
# Using split() + join() + list comprehension + len()
 
# initializing string
test_str = 'Gfg is best for all geeks'
 
# printing original string
print("The original string is : " + (test_str))
 
# initializing K
K = 3
 
# getting splits
temp = test_str.split()
 
# omitting K lengths
res = [ele for ele in temp if len(ele) != K]
 
# joining result
res = ' '.join(res)
 
# printing result
print("Modified String : " + (res))

Output:

The original string is : Gfg is best for all geeks
Modified String : is best geeks

Method #2 : Using filter() + lambda + split() + len() + join()

In this, we perform task of filtering using filter() + lambda, rest all the functionalities are similar to above method.

Python3




# Python3 code to demonstrate working of
# Remove K length words in String
# Using filter() + lambda + split() + len() + join()
 
# initializing string
test_str = 'Gfg is best for all geeks'
 
# printing original string
print("The original string is : " + (test_str))
 
# initializing K
K = 3
 
# getting splits
temp = test_str.split()
 
# omitting K lengths
# filtering using filter() and lambda
res = list(filter(lambda ele: len(ele) != K, temp))
 
# joining result
res = ' '.join(res)
 
# printing result
print("Modified String : " + (res))

Output:

The original string is : Gfg is best for all geeks
Modified String : is best geeks


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!