How to Remove Specific Words from a String in Python

To remove specific words from a string in Python, use re.sub() with word boundaries so only whole words go, then tidy the spaces. Plain replace() works too, but it also cuts the word out of longer words:

import re
re.sub(r"\bsale\b", "", "Big sale on sales tables").split()  # ['Big', 'on', 'sales', 'tables']

In this tutorial, we’ll compare four ways to do it, and I ran every script on Python 3.12.5 and pandas 3.0.6. Save each one as a .py file, open Command Prompt in that folder and run python file_name.py.

Remove a word with str.replace() and watch for partial matches

replace(old, "") deletes every occurrence of the text, wherever it sits. That’s fine for a unique word, but short words hide inside others:

text = "This listing is for a condo in Austin and it is pet friendly."

print(text.replace("is", ""))       # also hits "This" and "listing"
print(text.replace(" is", ""))      # safer here, still not bulletproof
print(text.replace(" is", "", 1))   # only the first match

Output:

Th lting  for a condo in Austin and it  pet friendly.
This listing for a condo in Austin and it pet friendly.
This listing for a condo in Austin and it is pet friendly.
Command Prompt output of Python str.replace removing the word is and also breaking This and listing
Replacing "is" turns This into Th and listing into lting; adding the leading space avoids that.

The optional third argument limits how many matches change. If you’re removing arbitrary text rather than words, my guide on removing a substring from a string in Python covers slicing and the other options.

How do I remove only whole words from a string?

Wrap the words in \b, the regex word boundary, so free can’t match inside Freestyle. re.escape() keeps characters like . or $ literal, and re.IGNORECASE catches any capitalization:

import re

subject = "URGENT: Act now for your free upgrade on Freestyle plans in Austin"
banned = ["urgent", "free", "act now"]

pattern = r"\b(?:" + "|".join(map(re.escape, banned)) + r")\b"
cleaned = re.sub(pattern, "", subject, flags=re.IGNORECASE)
print(repr(cleaned))

cleaned = re.sub(r"\s{2,}", " ", cleaned).strip(" :")   # tidy the gaps
print(repr(cleaned))

Output:

':  for your  upgrade on Freestyle plans in Austin'
'for your upgrade on Freestyle plans in Austin'
Command Prompt output of Python re.sub removing a list of whole words from an email subject and cleaning spaces
The first line shows the gaps left behind; the second is the cleaned subject, with Freestyle untouched.

Removing words always leaves extra spaces, so you’ll want the second pass. New to patterns? Start with regular expressions in Python, and see removing spaces from a string for more cleanup tricks.

Remove stopwords from a string with split() and join()

Stopwords are filler words like the, and or for. You split the text into words and keep the ones that aren’t in a set, with no regex needed. The catch is punctuation glued to a word:

import string

stop_words = {"the", "a", "an", "and", "of", "in", "to", "with"}
text = "Seattle has lots of cafes to work in. Pick one with fast Wi-Fi and a quiet corner."

naive = [w for w in text.split() if w.lower() not in stop_words]
print(" ".join(naive))

kept = [w for w in text.split()
        if w.strip(string.punctuation).lower() not in stop_words]
print(" ".join(kept))

Output:

Seattle has lots cafes work in. Pick one fast Wi-Fi quiet corner.
Seattle has lots cafes work Pick one fast Wi-Fi quiet corner.
Command Prompt output of Python removing stopwords from a title with split, a set and join
The naive version keeps in. because the period sticks to it; stripping punctuation before the check removes it too.

A set makes each lookup quick even with hundreds of stopwords. For more on the split step, see splitting a sentence into a list of words, or strip it all first with removing punctuation from strings.

Remove words from a pandas column

For a whole column, Series.str.replace() takes the same pattern. Pass regex=True and case=False, then rejoin the words to squeeze the spaces:

import pandas as pd

products = pd.Series([
    "NEW Wireless Mouse - Sale",
    "Laptop Stand (sale) new arrival",
    "USB-C Hub Renewed",
])

pattern = r"\b(?:new|sale)\b"
clean = (products.str.replace(pattern, "", regex=True, case=False)
                 .str.replace(r"[()]|\s-\s", " ", regex=True)
                 .str.split().str.join(" "))
print(clean.to_string())

Output:

0          Wireless Mouse
1    Laptop Stand arrival
2       USB-C Hub Renewed
Command Prompt output of pandas str.replace removing the words new and sale from product names
NEW and sale are gone in any case, while Renewed keeps its letters thanks to the word boundary.

Need to know whether a word is there before you touch it? Use the checks in checking if a Python string contains a substring.

Word-removal methods compared

MethodWhole words onlyBest for
s.replace(w, "")NoUnique words, quick fixes
re.sub(r"\bw\b", "", s)YesLists of words, any case
split() + set + join()YesStopwords, long word lists
Series.str.replace(..., regex=True)Yespandas columns

Keep going with these string cleanups:

Frequently asked questions

How do I remove a word from a string in Python?

Use re.sub(r"\bword\b", "", text) for whole words, or text.replace("word", "") when partial matches can’t happen.

How do I remove multiple words at once?

Join them into one pattern: r"\b(?:" + "|".join(map(re.escape, words)) + r")\b", then call re.sub() once.

How do I remove a word without matching it inside other words?

Use the \b word boundary in a regex, or split the string and compare whole words.

How do I make word removal case-insensitive?

Pass flags=re.IGNORECASE to re.sub(), or case=False in pandas.

How do I get rid of double spaces after removing words?

Use " ".join(text.split()) or re.sub(r"\s{2,}", " ", text).strip().

Where is re.sub() documented?

See re.sub() in the Python docs.