Last modified: Apr 10, 2023 By Alexander Williams

How to Remove Words from a String in Python: Methods and Examples

In this article, we'll explore different methods for removing words from a string in Python and provide examples to help you get started.

Method 1: Using string.replace() Method

The string.replace() method allows you to replace one or more occurrences of a substring with another. To remove a word from a string using this method, you can replace the word with an empty string:

string = "hello world"
word_to_remove = "world" 
new_string = string.replace(word_to_remove, "")

The code above replaces the word "world" with an empty string in the original "hello world" string. The result is a new string without the word "world":

Print the new string:

print(new_string) 

Output:

hello

Method 2: Using string.split() and string.join() Methods

The string.split() method splits a string into a list of substrings based on a delimiter. The string.join() method joins a list of strings into a single string.

To remove a word from a string using these methods, we need to follow these steps:

  1. Split the string into words.
  2.  Use a list comprehension to exclude the word you want to remove from the list of words.
  3. Join the remaining words back into a string using a space as a separator.

 

string = "hello world"
word_to_remove = "world"
new_string = " ".join([word for word in string.split() if word != word_to_remove])

The code splits the original string into words, removes the word "world" from the list, and then joins the remaining words back into a string.

The result is a new string without the word "world":

print(new_string) 

Output:

hello

Method 3: Using Regular Expressions with the re Library

The re library provides support for regular expressions in Python. To remove a word from a string using regular expressions, you can use the re.sub() function to replace the word with an empty string:

import re
string = "hello world"
word_to_remove = "world"
new_string = re.sub(r'\b{}\b'.format(word_to_remove), '', string)

The code above uses the re.sub() function with a regular expression that matches the word "world" surrounded by word boundaries (\b). The result is a new string without the word "world":

print(new_string) 

Output:

hello

Method 4: Using List Comprehension and Join Method

This method is similar to Method 2 but uses a list comprehension instead of a for loop.

To remove a word from a string using this method, you can split the string into words, remove the word you want to delete using a list comprehension, and then join the remaining words back into a string:

string = "hello world"
word_to_remove = "world"
new_string = " ".join([word for word in string.split() if word != word_to_remove])

The code above splits the original string into words, removes the word "world" using a list comprehension, and then joins the remaining words back into a string. The result is a new string without the word "world":

print(new_string) 

Output:

hello

Method 5: Using the Replace() Method in a Loop

This method uses a while loop to remove all occurrences of a word from a string. The loop replaces the word with an empty string until no more occurrences of the word are found in the string:

string = "hello world world"
word_to_remove = "world"
while word_to_remove in string:
string = string.replace(word_to_remove, "")

The code above removes all occurrences of the word "world" from the string "hello world world" using the replace() method in a loop. The result is a new string without the word "world":

print(new_string) 

Output:

hello

Conclusion

This article explored several methods to remove words from a string, including the replace() method, string.split() and string.join() methods, regular expressions with the re library, list comprehension and join method, and replace() method in a loop. These methods are efficient and easy to use and will help you to clean up your text data or prepare it for analysis.