How to Check if a Word is in a String in Python

To check if a word is in a string in Python, use the in operator for a quick test, or re.search() with \b word boundaries when it has to be the whole word, not part of a longer one:

"plot" in text                                   # quick substring test
re.search(r"\bplot\b", text, re.IGNORECASE)      # whole word, any case

I ran each script in Command Prompt on Python 3.12.5, with customer reviews, a support ticket and a billing email as test text.

You’ll see the in trap first, then whole-word checks with split() and regex, case, and several words at once.

Check a word with the in operator

in returns True when the text appears anywhere in the string. It’s a substring test, though, so a short word can match inside a longer one:

review = "The scatter plot made the data easy to read."

print("cat" in review)       # True  - 'cat' sits inside 'scatter'
print("plot" in review)      # True
print("chart" in review)     # False
print("chart" not in review) # True

Output:

True
True
False
True
Command Prompt output of the Python in operator returning True for cat inside scatter and False for chart
"cat" in review is True only because of the word scatter, while chart isn’t found at all.

If any match is fine, that’s enough. My guide on checking if a Python string contains a substring goes deeper.

Match whole words with split() and punctuation removal

A list of words makes in compare whole words. But split() only breaks on spaces, so fair! keeps its punctuation. Strip it with str.translate() first:

import string

review = "Shipping was fast, and the price ($24.99) was fair!"

words = review.lower().split()
print(words)
print("fair" in words)        # False - the word is 'fair!'

table = str.maketrans("", "", string.punctuation)
clean = review.lower().translate(table).split()
print(clean)
print("fair" in clean)        # True
print("ship" in clean)        # False - whole words only

Output:

['shipping', 'was', 'fast,', 'and', 'the', 'price', '($24.99)', 'was', 'fair!']
False
['shipping', 'was', 'fast', 'and', 'the', 'price', '2499', 'was', 'fair']
True
False
Command Prompt output of Python split leaving punctuation on words, then translate removing it so the whole-word check works
With punctuation left on, fair isn’t found; after translate() it is, and ship doesn’t match shipping.

It’s fine for plain text. More splitting options are in splitting a sentence into a list of words in Python.

How do I check for an exact word with regex?

A regex with \b on both sides matches only the whole word, and punctuation is handled for you. re.escape() stops . or + acting as regex symbols:

import re

def has_word(text, word):
    pattern = rf"\b{re.escape(word)}\b"
    return re.search(pattern, text, flags=re.IGNORECASE) is not None

ticket = "Customer in Austin, TX asked for a Refund on order #4521 (May 4, 2026)."

print(has_word(ticket, "refund"))   # True  - case ignored
print(has_word(ticket, "fun"))      # False - inside 'Refund'
print(has_word(ticket, "TX"))       # True  - next to a comma
print(has_word(ticket, "order"))    # True

m = re.search(r"\brefund\b", ticket, flags=re.IGNORECASE)
print(m.group(), "found at index", m.start())

Output:

True
False
True
True
Refund found at index 35
Command Prompt output of a Python regex word-boundary function finding refund, TX and order but not fun inside Refund
The word-boundary check finds refund in any case and TX beside a comma, but rejects fun inside Refund.

m.start() gives the position. For every position, see finding all occurrences of a substring in Python.

Make the word check case-insensitive

in and re.search() are case-sensitive. With in, use casefold() (a stronger lower()); with regex, pass re.IGNORECASE:

"Refund" in ticket                       # case-sensitive
"refund" in ticket.casefold()            # case-insensitive

Comparing whole strings without case is a separate job, covered in comparing strings in Python.

Check if a string contains any or all of several words

For a keyword list, collect the matches, then use any() or all(). re.findall() counts whole words; find() returns -1 on no match:

import re

def found_words(text, words):
    return [w for w in words if re.search(rf"\b{re.escape(w)}\b", text, re.IGNORECASE)]

email = "Your invoice for $149.00 is past due. Please pay by June 30, 2026."
keywords = ["invoice", "due", "refund", "pay"]

print(found_words(email, keywords))
print("any:", any(w in found_words(email, keywords) for w in keywords))
print("all:", all(w in found_words(email, keywords) for w in keywords))

print("count of 'pay':", len(re.findall(r"\bpay\b", email, re.IGNORECASE)))
print("find('due'):", email.find("due"), " find('late'):", email.find("late"))

Output:

['invoice', 'due', 'pay']
any: True
all: False
count of 'pay': 1
find('due'): 33  find('late'): -1
Command Prompt output of Python checking a list of keywords in an email with any, all, findall and find
Three of the four keywords are found, so any() is True and all() is False because refund is missing.

To delete a word once you’ve found it, see removing specific words from a string in Python.

You might also find these string checks handy:

Frequently asked questions

How do I check if a word is in a string in Python?

Use "word" in text for a quick check, or re.search(r"\bword\b", text) to match only the whole word.

Why does “cat” in text return True when the word isn’t there?

in checks substrings, so it matches cat inside scatter. Use a \b regex for whole words.

How do I check for a word without caring about case?

Use "word" in text.casefold(), or pass re.IGNORECASE to re.search().

How do I check if a string contains any word from a list?

Use any(w in text for w in words).

How do I find the position of a word in a string?

text.find(word) returns the first index or -1; re.search() gives it through .start().

Where is the \b word boundary documented?

See the re module in the Python docs.