HOWTO · Python

How to Check if a String Contains Word in Python

The article will discuss different ways on how to check the string if it contains a word in Python.

On this page

To check whether text occurs in a Python string, use the in operator. It returns a Boolean and is case-sensitive.

Use the if/in Statement in Python to Check the String if It Contains a Word

For a simple substring check, use in directly in an if condition. It returns a Boolean, and the comparison is case-sensitive.

text = "This contains a word"
if "word" in text:
    print("Found")
else:
    print("Not Found")

The operator checks for a sequence of characters anywhere in the string. It does not require a whole-word match: "is" in "This" is true because those characters occur at the end of This. This behavior can produce a false positive if the task is specifically to find a separate word.

For text where words are separated only by ordinary spaces, adding a space at each end and searching for a space-wrapped term can be a small workaround: " is " in (" " + text + " "). It does not handle punctuation, tabs, line breaks, or other separators consistently. Use a pattern with boundaries or tokenize the text when those separators matter.

Use the find() Method in Python to Check the String if It Contains a Word

find() also searches for a substring and returns its first starting index. If it cannot find the requested text, it returns -1. Compare the result explicitly with -1; do not use the index itself as a condition. Index 0 is falsey, while -1 is truthy.

text = "word appears here"
position = text.find("word")
if position != -1:
    print("Found at index", position)
else:
    print("Not Found")

This correctly recognizes a match at the beginning of the string. Like in, find() performs substring matching, not whole-word matching. Choose it when you need the location as well as a presence check.

Use Regular Expressions in Python to Check the String if It Contains a Word

Use re.search() when the search needs a pattern, such as matching a term surrounded by word boundaries. In a regular expression, \b marks a transition between a word character and a non-word character.

import re

text = "This contains a word."
if re.search(r"\bword\b", text):
    print("Found")
else:
    print("Not Found")

The raw pattern r"\bword\b" matches word as a separate regex word under Python’s \w rules. Those rules may not match every application’s definition of a word, particularly for specialized tokenization or language-specific boundaries. For ordinary substring checks, in is simpler.

Use the index() Method With Exception Handling in Python to Check the String if It Contains a Word

index() returns the first matching index just like find(), but it raises ValueError when the substring is absent instead of returning -1. Handle that exception when absence is an expected possibility and the index is useful when a match exists.

text = "This contains a word"
try:
    position = text.index("word")
    print("Found at index", position)
except ValueError:
    print("Not Found")

The try block handles a successful lookup; the except ValueError block handles a missing substring. If you only need a Boolean, in communicates that intent more directly.

Conclusion

Use in for ordinary substring presence, and remember that it is case-sensitive and can match characters inside a larger word. Use a boundary-aware re.search() pattern when its definition of a word fits the text. Choose find() when you want an index and a -1 result for absence, or index() when a missing match should be handled through ValueError.