Tutorial

Python String contains

Practical guide to checking whether a Python string contains a substring using in, __contains__, find/index, case-insensitive methods, multiple-substring checks, and regex.

Drake Nguyen

Founder · System Architect

3 min read
Python String contains
Python String contains

Python string contains: membership tests and methods

This guide explains how to determine whether one string appears inside another in Python. It covers the recommended approaches for python string contains checks, including the in operator, the python __contains__ method, find/index, case-insensitive searches, and checking for multiple substrings or whole words.

Using the in operator (recommended)

The simplest and most Pythonic way to test whether a substring exists is the in operator. It returns a boolean and reads clearly in code.

s = "Hello, Python world"
print("Python" in s)   # True
print("python" in s)   # False (case sensitive)

Using the __contains__ method

Every str implements __contains__, which the in operator calls under the hood. You can call it directly, but using in is preferred for readability.

s = "abc"
print(s.__contains__("a"))    # True
print(str.__contains__("ABC", "A"))  # True

Using find() and index()

find() returns the lowest index of the substring or -1 if not found; index() raises a ValueError if the substring is missing. These are useful when you need the position rather than a boolean.

s = "look for a needle in a haystack"
print(s.find("needle") != -1)   # True  -> python check substring by index
print(s.index("needle"))        # returns the index (raises if not present)

Case-insensitive checks

To perform a python string contains substring case insensitive search, normalize the strings. Use lower() or casefold() for better Unicode support.


needle = "quick"
# Simple approach
print(needle.lower() in haystack.lower())
# Better for Unicode
print(needle.casefold() in haystack.casefold())

Checking for multiple substrings

To see if a string contains any or all of several substrings, use any() or all() with a generator expression.

text = "Read the documentation before coding"
keywords = ["docs", "documentation", "guide"]
# Contains any
print(any(k in text for k in keywords))
# Contains all
print(all(k in text for k in ["Read", "coding"]))

Check if a string contains a whole word

If you need to check for whole words (not substrings inside words), regular expressions are useful.

import re
text = "Python string membership test"
# \b ensures word boundaries
print(bool(re.search(r"\bstring\b", text)))   # True

When to use which method

  • Use the in operator for clear, readable python check if string contains substring tests.
  • Call __contains__ directly only in special cases; it behaves like in and is case sensitive.
  • Use find() or index() when you need the position of the substring.
  • Use casefold()/lower() for python string contains substring case insensitive checks.
  • Use any()/all() to check python string contains multiple substrings.
  • Use regex when you need pattern matching or whole-word detection.
Summary: For most use cases, the in operator is the best choice for a python string contains test. For case-insensitive checks use casefold(); for multiple checks use any()/all(); and for word-level or pattern checks, use regular expressions.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.