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

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.

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.
Related

Amazon CloudWatch allows you to run log insights queries using the logs client in Boto3. Below is a step by step guide to querying logs…

28 Oct, 2025

The Kalman Filter is an efficient recursive algorithm used to estimate the state of a system from noisy measurements. In computer vision, it’s commonly used…

21 Oct, 2025

Printing exceptions in python helps to debug issues by giving clear information about what went wrong and where. You can access the exception message or…

15 Oct, 2025