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., , ILI have executed the above example code and added the screenshot below.

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. ILI have executed the above example code and added the screenshot below.

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., , ILI have executed the above example code and added the screenshot below.

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 ILA 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 , ILThe 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:
- How to Check if a String is Bytes in Python?
- How to Check if a String is Surrounded by Quotes in Python?
- How to Check if a String is in CamelCase Format Using Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.