Python program to remove K length words in String
Last Updated :
18 Feb, 2023
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
test_str = 'Gfg is best for all geeks'
print ( "The original string is : " + (test_str))
K = 3
temp = test_str.split()
res = [ele for ele in temp if len (ele) ! = K]
res = ' ' .join(res)
print ( "Modified String : " + (res))
|
Output:
The original string is : Gfg is best for all geeks
Modified String : is best geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
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
test_str = 'Gfg is best for all geeks'
print ( "The original string is : " + (test_str))
K = 3
temp = test_str.split()
res = list ( filter ( lambda ele: len (ele) ! = K, temp))
res = ' ' .join(res)
print ( "Modified String : " + (res))
|
Output:
The original string is : Gfg is best for all geeks
Modified String : is best geeks
The time and space complexity for all the methods are the same:
Time Complexity : O(n)
Auxiliary Space : O(n)
Method #3: # Using split() + join() + remove() + len()
Python3
test_str = 'Gfg is best for all geeks'
print ( "The original string is : " + (test_str))
K = 3
temp = test_str.split()
for i in temp.copy():
if len (i) = = K:
temp.remove(i)
res = ' ' .join(temp)
print ( "Modified String : " + (res))
|
Output
The original string is : Gfg is best for all geeks
Modified String : is best geeks
Time Complexity : O(n)
Auxiliary Space : O(n)
Method #4: # Using recursive method.
Python3
def remove_string(start,lst,k,word = ''):
if start = = len (lst):
return word
if len (lst[start])! = k:
word = word + lst[start] + ' '
return remove_string(start + 1 ,lst,k,word)
test_str = 'Gfg is best for all geeks'
print ( "The original string is : " + (test_str))
K = 3
test_str = test_str.split()
res = remove_string( 0 ,test_str,K)
print ( "Modified String : " + (res))
|
Output
The original string is : Gfg is best for all geeks
Modified String : is best geeks
Time Complexity : O(n)
Auxiliary Space : O(n)
Approach #5: Using Regular Expression
The given program removes all the words with length K from a given input string. Here is a step-by-step approach to implement the same program:
1. Import the ‘re’ module for using regular expressions.
2. Initialize a string variable test_str with the given string.
3. Initialize a variable K with the given value.
4. Use the re.sub() method to replace all the words with length K with an empty string.
5. Split the resulting string by space using the split() method, and then join the list using the join() method to remove extra whitespaces between words.
6. Print the modified string.
Note that \b in the regular expression pattern matches a word boundary, and \w matches a word character (alphanumeric or underscore). The { and } characters in the pattern are used to specify the number of repetitions, which is taken as the value of the K variable converted to a string using str(K).
Python3
import re
test_str = 'Gfg is best for all geeks'
print ( "The original string is : " + (test_str))
K = 3
res = " " .join(re.sub(r '\b\w{' + str (K) + r '}\b' , '', test_str).split())
print ( "Modified String : " + (res))
|
Output
The original string is : Gfg is best for all geeks
Modified String : is best geeks
Time Complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(n), where n is the number of characters in the result.
In this approach, we are using the re module in python to perform regular expression operations. The ‘\b’ matches word boundaries, the ‘\w’ matches word characters and the curly braces ‘{}’ specify the number of repetitions. So, the regular expression ‘\b\w{‘+ str(K) + r’}\b’ matches all the words with length K. The ‘sub’ function replaces all the matches with an empty string.
Share your thoughts in the comments
Please Login to comment...