Python program to remove K length words in String


In this article, we will learn a python program to remove K-length words in String.

Methods Used

The following are the various methods to accomplish this task −

  • Using split(), join(), len() and list comprehension

  • Using filter(), lambda(), split() and len() functions

  • Using split(), join(), remove() and len() functions

Example

Assume we have taken an input string and k length. We will now remove all the given k-length words from the input string using the above methods.

Input

inputString = "hello tutorialspoint python codes"
k_length = 5

Output

Resultant string after removing 5 length words:
tutorialspoint python

In this example, the given k length is 5, hence all the words having length 5 such as hello, codes are removed from the input string and the resultant string is printed.

Method 1: Using split(), join(), len() and list comprehension

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

join() − join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input string and print the given string.

  • Create another variable to store k length.

  • Use the split() function(splits a string into a list. We can define the separator; the default separator is any whitespace) to split the input string into a list of words

  • Traverse through the words list and check whether the length of the current element is not equal to the input k length and store it in a list.

  • Use the join() function to convert the list into a string.

  • Print the resultant string after removing all the given k-length words from an input string.

Example

The following program returns a string after removing all the input k-length words from an input string using split(), join(), len() functions and list comprehension –

# input string
inputString = "hello tutorialspoint python codes"

# printing the input string
print("Input string: ", inputString)

# input k length value
k_length = 5

# splitting the input string into a list of words
wordsList = inputString.split()

# traversing through the words list and checking whether the length of

# current string is not equal to input k length and storing it in a list
resultList = [element for element in wordsList if len(element) != k_length]

# joining the above list to a string to convert it to a string
resultString = ' '.join(resultList)

# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)

Output

On executing, the above program will generate the following output –

Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python

Method 2: Using filter(), lambda(), split() and len() functions

filter() function − filters the specified sequence using a function that determines if each element in the sequence is to be true or false.

Lambda Function

Lambda Function, often known as an 'Anonymous Function,' is the same as a normal Python function except that it can be defined without a name. The def keyword is used to define normal functions, while the lambda keyword is used to define anonymous functions. They are, however, limited to a single line of expression. They, like regular functions, can accept several parameters.

Syntax

lambda arguments: expression

Example

The following program returns a string after removing all the input k-length words from an input string using filter(), lambda(), split(), and len() functions –

# input string
inputString = "hello tutorialspoint python codes"

# printing the input string
print("Input string: ", inputString)

# input k length value
k_length = 5

# Splitting the input string into a list of words
wordsList = inputString.split()

# Traverse in the given words list and filter the list elements

# if the length of the element is not equal to k using the lambda function.
resultList = filter(lambda element: len(element) != k_length, wordsList)

# Converting filter object to the list
resultList = list(resultList)

#joining the above list to a string to convert it to a string
resultString = ' '.join(resultList)

# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)

Output

On executing, the above program will generate the following output –

Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python

Method 3: Using split(), join(), remove() and len() functions

split() − splits a string into a list. We can define the separator; the default separator is any whitespace.

remove() method − removes the first occurrence of the element with the given value.

Syntax

list.remove(obj)

Example

The following program returns a string after removing all the input k-length words from an input string using split(), join(), remove() and len() functions –

# input string
inputString = "hello tutorialspoint python codes"

# printing the input string
print("Input string: ", inputString)

# input k length value
k_length = 5

# splitting the input string into a list of words
wordsList = inputString.split()

# making a copy of the words list and traveling in that list
for element in wordsList.copy():
   
   # checking whether the length of current is equal to the input k length
   if len(element) == k_length:
      
      # removing that element if the condition is true
      wordsList.remove(element)
      
# joining words list to string to convert to a string
resultString = ' '.join(wordsList)

# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)

Output

On executing, the above program will generate the following output –

Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python

Conclusion

In this article, we learned how to remove k-length terms using three distinct methods from the given word list. Using the lambda and filter functions, we have also learned how to filter the list's elements according to the specified condition.

Updated on: 27-Jan-2023

372 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements