Does Python Have a String ‘contains’ Substring Method?

There's no .contains() method in Python, but you can check for substrings using the in keyword or built-in string methods like .find() and .index().

Summarize with AI

Not enough time? get the key points instantly.

Python string does not have a built-in .contains() method like some other languages (e.g., Java), but you can easily check if a substring exists using below methods.

Method 1: Use the in Keyword (Recommended)

This is the most Pythonic way to check if a substring exists in a string.

python

text = “hello world”if “world” in text:

    print(“Yes, found!”)

Best for clean, readable code in most use cases.

Method 2: Use .find() Method

Returns the starting index of the substring if found, or -1 if not found.

python

text = “hello world”if text.find(“world”) != -1:

    print(“Found using .find()”)

Useful when you need the position of the substring.

Also Read: Pros & Cons of Python Web Development in 2025

Method 3: Use .index() Method

Similar to .find(), but raises a ValueError if the substring is not found.

python

text = “hello world”try:

    index = text.index(“world”)

    print(“Found at index:”, index)

except ValueError:

    print(“Substring not found”)

Use this if you’re okay handling exceptions.

Also Read:

Tip

Always prefer the in keyword for simplicity. Use .find() or .index() only when you need the index position or want to handle absence explicitly.


Resources

Insights from our engineering leaders.

Our team shares practical insights on the technologies and engineering decisions shaping enterprise software today, grounded in real client work.

Python

How to Copy Files in Python

You can copy files in python using the shutil module which provides high level file and collection of files operations. You can also combine it…