Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to remove K length words in String
In this article, we will learn how to remove all words of a specific length (K) from a string using Python. This is useful for text processing tasks where you need to filter words based on their character count.
Problem Statement
Given an input string and a value K, we need to remove all words that have exactly K characters and return the modified string.
Example
inputString = "hello tutorialspoint python codes" k_length = 5
The output will be ?
Resultant string after removing 5 length words: tutorialspoint python
In this example, words like hello and codes (both 5 characters long) are removed, leaving tutorialspoint and python.
Using List Comprehension with split() and join()
This approach splits the string into words, filters out K-length words using list comprehension, then joins the remaining words back into a string ?
# Input string and K value
inputString = "hello tutorialspoint python codes"
k_length = 5
print("Input string:", inputString)
# Split string into words and filter out K-length words
words = inputString.split()
filtered_words = [word for word in words if len(word) != k_length]
# Join filtered words back into a string
result = ' '.join(filtered_words)
print("Resultant string after removing", k_length, "length words:")
print(result)
Input string: hello tutorialspoint python codes Resultant string after removing 5 length words: tutorialspoint python
Using filter() with Lambda Function
The filter() function with a lambda expression provides a functional programming approach to filter words ?
# Input string and K value
inputString = "hello tutorialspoint python codes"
k_length = 5
print("Input string:", inputString)
# Split and filter using filter() and lambda
words = inputString.split()
filtered_words = list(filter(lambda word: len(word) != k_length, words))
# Join back to string
result = ' '.join(filtered_words)
print("Resultant string after removing", k_length, "length words:")
print(result)
Input string: hello tutorialspoint python codes Resultant string after removing 5 length words: tutorialspoint python
Using Loop with remove() Method
This approach iterates through a copy of the word list and removes K-length words using the remove() method ?
# Input string and K value
inputString = "hello tutorialspoint python codes"
k_length = 5
print("Input string:", inputString)
# Split into words
words = inputString.split()
# Iterate through copy and remove K-length words
for word in words.copy():
if len(word) == k_length:
words.remove(word)
# Join remaining words
result = ' '.join(words)
print("Resultant string after removing", k_length, "length words:")
print(result)
Input string: hello tutorialspoint python codes Resultant string after removing 5 length words: tutorialspoint python
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension | High | Fast | Most Python use cases |
| filter() + lambda | Medium | Fast | Functional programming style |
| Loop + remove() | High | Slower | When modifying original list |
Conclusion
List comprehension is the most Pythonic and efficient approach for removing K-length words from a string. Use filter() with lambda for functional programming style, and the loop method when you need to modify the original list in place.
