To python check substring in string, use the in operator: "cat" in "concatenate" returns True when the text appears anywhere in the string and False otherwise. It is the shortest and most readable way, and it is case sensitive. When you also need the position of the match, use find() or index().
This article shows in, startswith(), endswith(), find(), count() and regular expressions, with the case-insensitive version. Examples target Python 3.13 and 3.14.
The in operator tests whether the left string is a substring of the right string:
Use it directly in a condition:
An empty string is a substring of every string, so "" in "abc" is True. The in operator also works for lists and sets, but with a string on the right it checks for a contiguous run of characters, not for separate items.
"Fox" in "The quick brown fox" is False because of the capital letter. For a case-insensitive check, normalise both sides. Prefer casefold() over lower() because it handles more languages:
When the substring must be at one end, use these methods. They also accept a tuple of options:
This is more precise than in, since ".png" in "my.png.txt" is True even though the file is a text file.
find() returns the lowest index of the match, or -1 when it is missing. It never raises an error:
The second argument is where to start searching. Do not write if s.find("an"): because index 0 is falsy and -1 is truthy, which gives the wrong answer. Compare explicitly: if s.find("an") != -1, or better just use in when the position does not matter. The index() method does the same search but raises ValueError when nothing is found.
count() tells you how many non-overlapping matches exist:
When you need a pattern rather than fixed text, use the re module:
re.search() returns a match object or None. Use it for digits, optional parts or alternatives such as cat|dog. If you search for text containing special characters, escape it with re.escape(). For plain fixed text, in is simpler and faster.
Real checks often combine tests. To require all of several words, use all(); to accept any one, use any():
A common mistake is writing "a" or "b" in text. Python reads this as "a" or ("b" in text), and a non-empty string is always truthy, so the condition is always true. Write "a" in text or "b" in text, or use any().
in matches characters, not words, so "cat" in "concatenate" is True. To match a whole word, split the text into words, or use a regular expression with word boundaries:
This matters for filters and search boxes, where partial matches inside longer words are usually unwanted.
| Goal | Use |
|---|---|
| Does the text appear anywhere? | needle in text |
| Does it start or end with it? | startswith() or endswith() |
| Where is the first match? | find() or index() |
| How many matches? | count() |
| Match a pattern or ignore case in complex ways | re.search() |
| Does any of several words appear? | any(w in text for w in words) |
The in operator is the standard and fastest approach for plain text, because CPython runs a tuned search routine behind it.
There is no str.contains(). The in operator calls the special method __contains__, which you should not call directly.
Apply casefold() or lower() to both strings before using in, or use re.search() with re.IGNORECASE.
Use in when you only need a yes or no. Use find() when you also need the index.
Use substring in text to check containment, startswith() and endswith() for the ends, find() when you need the position and re.search() for patterns. Remember that matching is case sensitive unless you normalise the text first.