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 Replace all words except the given word
In this article, we will learn how to replace all words except the given word in a string in Python. This technique is useful for text processing where you want to mask certain words while preserving specific keywords.
Methods Used
The following are the various methods to accomplish this task:
Using For Loop, split() & join() Functions
Using List Comprehension
Problem Statement
Given an input string, we need to replace all words with a replacement character except for one specific word that should remain unchanged.
Input
inputString = 'hello tutorialspoint python codes' inputWord = "tutorialspoint" replaceChar = "@"
Expected Output
@ tutorialspoint @ @
In this example, all words are replaced with "@" symbol except "tutorialspoint" which remains unchanged.
Method 1: Using For Loop, split() and join() Functions
This method uses a traditional for loop approach with string manipulation functions:
split() Splits a string into a list of words using whitespace as delimiter
join() Joins elements of a list into a single string with specified separator
Example
# input string
inputString = 'hello tutorialspoint python codes'
print("Input String:", inputString)
# input word to preserve
inputWord = "tutorialspoint"
# replacement character
replaceChar = "@"
# split input string into list of words
wordsList = inputString.split(" ")
# traverse through each word
for index in range(len(wordsList)):
element = wordsList[index]
# replace word if it's not the input word
if element != inputWord:
wordsList[index] = replaceChar
# join the words back into a string
resultantString = " ".join(wordsList)
print("Replacing all words except input word with '@':")
print(resultantString)
Input String: hello tutorialspoint python codes Replacing all words except input word with '@': @ tutorialspoint @ @
Time Complexity: O(n) where n is the number of words
Space Complexity: O(n) for storing the word list
Method 2: Using List Comprehension
List comprehension provides a more concise and Pythonic way to achieve the same result in a single line.
Example
# input string
inputString = 'hello tutorialspoint python codes'
print("Input String:", inputString)
# input word to preserve
inputWord = "python"
# replacement character
replaceChar = "*"
# create new list using list comprehension
resultantList = [element if element == inputWord else replaceChar
for element in inputString.split()]
# join the list back to string
resultantString = " ".join(resultantList)
print("Replacing all words except input word with '*':")
print(resultantString)
Input String: hello tutorialspoint python codes Replacing all words except input word with '*': * * python *
Time Complexity: O(n) where n is the number of words
Space Complexity: O(n) for the result list
Comparison
| Method | Readability | Lines of Code | Best For |
|---|---|---|---|
| For Loop | More explicit | More lines | Complex logic |
| List Comprehension | Concise | Single line | Simple transformations |
Conclusion
Both methods effectively replace all words except a specified word in a string. Use list comprehension for concise code and for loops when you need more complex logic or better readability for beginners.
