How to remove specific words from a string in Python?

As a developer, working on projects for one of my clients, I encountered a scenario where I needed to remove specific words from a string in Python. After research, I found five efficient methods to accomplish this task. I will help you to learn all the methods by giving suitable examples and screenshots of executed example code.

Remove specific words from a string in Python

Python provides various ways to remove specific words from a string. Let us see some important methods.

Read How to Convert a String to an Integer in Python?

Method 1. Use replace() Function

The replace() function in Python is used to replace a substring with another substring. To remove specific words, you replace them with an empty string.

address = "123 Main St., Springfield, IL"
updated_address = address.replace("Springfield", "")
print(updated_address) 

Output:

123 Main St., , IL

I have executed the above example code and added the screenshot below.

remove specific words from a string in Python

The replace() function takes two arguments: the word to be replaced ("Springfield") and the word to replace it with (in this case, an empty string "").

Check out How to Check if a String is an Emoji in Python?

Method 2. Use List and join()

By splitting the string into a list of words and filtering out the word you want to remove, you can then reassemble the string using join().

address = "123 Main St., Springfield, IL"
words = address.split()  
updated_address = " ".join([word.strip(",") for word in words if word.strip(",") != "Springfield"])
print(updated_address) 

Output:

123 Main St. IL

I have executed the above example code and added the screenshot below.

How to remove specific words from a string in Python

The split() method splits the string into a list of words. The join() method is then used to combine the remaining words into a string with spaces in between.

Read How to Check if a String Contains Any Special Character in Python?

Method 3. Use Regular Expressions

The re.sub() method from the re module allows you to replace patterns (including words) in a string. You can use regular expressions to match and remove specific words.

import re

address = "123 Main St., Springfield, IL"
updated_address = re.sub(r"\bSpringfield\b", "", address)
print(updated_address) 

Output:

123 Main St., , IL

I have executed the above example code and added the screenshot below.

remove specific words from a string in Python re

The re.sub() function is used to search for the word "Springfield" (using word boundaries \b to match the whole word).

Check out How to Check if String Length is Greater Than 0 in Python?

Method 4. Use Set and join()

You can convert the list of words into a set and use join() to remove specific words. This method can be useful if you want to remove multiple words at once.

address = "123 Main St., Springfield, IL"
remove_words = {"Springfield", "St."}  # Set of words to remove
words = address.split()
updated_address = " ".join([word for word in words if word not in remove_words])
print(updated_address)

Output:

123 Main IL

A set of words to be removed (remove_words) is defined. The join() method combines the filtered words back into a single string.

Read How to Check if a String is Comma Separated in Python?

Method 5. Use For Loop and replace()

You can loop through a list of words to remove and apply the replace() function to each one.

address = "123 Main St., Springfield, IL"
remove_words = ["Springfield", "St."]
for word in remove_words:
    address = address.replace(word, "")
print(address) 

Output:

123 Main , IL

The list remove_words contains the words to be removed. A for loop iterates through each word in the list and uses replace() to remove it. After the loop, the string has had all specified words removed.

Check out How to Check if a String is a GUID in Python?

Conclusion:

In this article, I have explained to you how to remove specific words from a string in Python. I covered five methods to achieve this task such as the replace() function, list and join(), regular expression, set and join(), for loop and replace().

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.