DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

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

Python’s `in` operator checks for a substring. For case-insensitive searches, use `casefold()`; for standalone words, define a boundary rule with regular expressions.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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:

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().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.