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.

Methods Used

The following are the various methods to accomplish this task −

  • Using For Loop, split() & join() Functions

  • Using List Comprehension

Example

Assume we have taken an input string, input word, and the input character with which to be replaced. We will now replace all the words in an input string with the given input replace character expect the given word using the above methods.

Input

inputString = 'hello tutorialspoint python codes'
inputWord = "tutorialspoint"
replaceChar = "@"

Output

Replacing all words of string except input word with '@':
@ tutorialspoint @ @

In this example, the given input word is "tutorialspoint". Hence all the characters in an input string are replaced with “@” symbol except the given word "tutorialspoint".

Method 1: Using For Loop, split() & join() Functions

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.

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

Algorithm (Steps)

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

  • Create a variable to store the input string.

  • Print the input string.

  • Create another variable to store the input word.

  • Give the replace character (with which to be replaced).

  • Use the split() function to split the input string into the list of words.

  • Use the for loop to traverse till the length of the word list using len() function( returns the number of items in an object).

  • Get the list element at current index.

  • Use the if conditional statement to check whether the current element is not equal to the input word(Here tutorialspoint).

  • Replace the current element with the given replacing character if the condition is true.

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

  • Print the resultant string after replacing all words of an input string except the input word with input replacing character.

Example

The following program replaces all the words of an input string except the given word with input replace character(@) using the for loop, split(), and join() functions –

# input string
inputString = 'hello tutorialspoint python codes'

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

# input word
inputWord = "tutorialspoint"

# input replacing character (with which to be replaced)
replaceChar = "@"

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

# traversing till the length of the words list
for index in range(len(wordsList)):

   # getting the list element at current index
   element = wordsList[index]
   
   # checking whether the current element is not equal to the input word(tutorialspoint)
   if not element == inputWord:
      
      # assigning the input replacing character to the current element
      
      # if the condition is true
      wordsList[index] = replaceChar
      
# converting words list to a string using the join() function
resultantString = " ".join(wordsList)

# printing resultant string after replacing all words of the string

# except input word with input replacing character
print("Replacing all words of string except input word with '@':\n", resultantString)

Output

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

Input String: hello tutorialspoint python codes
Replacing all words of string except input word with '@':
@ tutorialspoint @ @

Time Complexity − O(n)

Auxiliary Space − O(n)

Method 2: Using 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.

This is another method for doing this task. In this method, we iterate for elements and do the task with a one-liner that has similar functionality to the previous method.

Example

The following program replaces all the words of an input string except the given word with input replace character(*) using list comprehension –

# input string
inputString = 'hello tutorialspoint python codes'

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

# input word
inputWord = "python"

# input replacing character (with which to be replaced)
replaceChar = "*"

# Here we create a new list using list comprehension

# Where if the element(word) is equal to the input word then we add a word to the list

# Else we add replace character to the list
resultantList = [replaceChar if not element == inputWord else element for element in inputString.split()]

# Join the list of words with spaces to make it a string
resultantString = " ".join(resultantList)

# printing resultant string after replacing all words of string

# except input word with input replacing character
print("Replacing all words of string except input word with '*':\n", resultantString)

Output

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

Input String: hello tutorialspoint python codes
Replacing all words of string except input word with '*':
* * python *

Time Complexity − O(n)

Auxiliary Space − O(n)

Conclusion

We studied two different methods in this article for replacing all words except the specified word. We also learned how to use list comprehension to do any task using simple and concise syntax.

Updated on: 27-Jan-2023

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements