Use Python’s in operator to check whether one string occurs inside another: word in text. It returns True for a matching substring and False otherwise. If “word” means a standalone token rather than any matching characters, use a boundary rule instead.
Check whether a substring is present
Put the text you are searching for on the left and the string to search in on the right:
As an Amazon Associate I earn from qualifying purchases.
word = "cat"
text = "The cat sat on the mat."
if word in text:
print("Found")
For strings, Python’s language reference defines x in y as true exactly when x is a substring of y. The order matters: word in text checks whether word occurs in text; reversing the operands asks a different question. See the Python language reference on membership tests.
Recommended Free Tools
Decide whether you mean a substring or a whole word
The in operator checks consecutive characters, not linguistic words. For example, "cat" in "concatenate" is true. If the match should be a standalone token, use an explicit boundary rule. A common option is a regular expression:
#1 Best Overall
import re
text = "A cat sleeps."
found = re.search(r"bcatb", text) is not None
In Python’s default Unicode regular-expression behavior, w matches Unicode alphanumeric characters and underscore, while b marks a transition between w and W. That means an underscore counts as part of a “word” for this boundary rule; for example, bcatb will not treat the cat in cat_name as a standalone word. Choose boundaries that fit your text, especially when punctuation, identifiers, or non-English text is involved. See the Python regular-expression documentation.
Make a case-insensitive substring check
Ordinary string membership is case-sensitive, so "cat" in "CAT" is false. For Unicode-aware caseless matching, casefold both strings before checking:
Rank #2
word = "straße"
text = "The STRASSE is busy."
found = word.casefold() in text.casefold()
str.casefold() is intended for caseless matching and is more aggressive than lowercasing; for instance, "straße".casefold() becomes "strasse". Because casefolding can change string length, an index in the transformed string is not necessarily an index into the original text. See the Python documentation for str.casefold().
Choose in or find()
Use in when you only need a yes-or-no answer. Use find() when you also need the first matching position:
text = "The cat sat on the mat."
position = text.find("cat")
if position != -1:
print("Found at index", position)
find() returns the index of the first occurrence or -1 when it is absent. For a Boolean result, text.find(word) != -1 is equivalent to word in text, but in expresses the existence check more directly. The equivalence is documented in the Python language reference.
Handle an empty search string deliberately
An empty string is considered a substring of every string, so "" in "abc" is True. If the searched value might be empty, decide whether that should count as a match and validate it explicitly when it should not:
Quick Recap
Best Value
if word and word in text:
print("Found a non-empty word")
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




