Python
string methods
substring search
programming
Python tips

Does Python have a string 'contains' substring method?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Python does not have a dedicated contains() string method like some other languages. Instead, the normal and idiomatic way to check whether one string contains another is the in operator.

Use in for Presence Checks

If all you want to know is whether a substring exists, in is the best answer.

python
1text = "Hello, world!"
2
3print("world" in text)
4print("python" in text)

This returns True for "world" and False for "python".

The reason in is preferred is simple:

  • it is readable
  • it returns a boolean directly
  • it expresses intent clearly

In Python, this is the closest equivalent to a “contains substring” method.

Use find() When You Need the Position

If you also want to know where the substring starts, use find().

python
1text = "Hello, world!"
2
3print(text.find("world"))
4print(text.find("python"))

find() returns:

  • the starting index if found
  • '-1 if not found'

That makes it useful when your next step depends on the location of the match rather than just its existence.

But if you only need a yes or no answer, in is usually cleaner than checking whether find() returned -1.

Use index() When Missing Data Should Be an Error

index() looks similar to find(), but the behavior differs when the substring is missing.

python
text = "Hello, world!"

print(text.index("world"))

If the substring exists, you get the starting position. If it does not exist, Python raises ValueError.

That can be useful when the substring is expected to exist and failure should be explicit. It is less convenient for simple membership checks because you need exception handling if the value might be missing.

python
1text = "Hello, world!"
2
3try:
4    position = text.index("python")
5    print(position)
6except ValueError:
7    print("Substring not found")

So the practical rule is:

  • use in for presence
  • use find() for safe position lookup
  • use index() when absence is exceptional

Use count() When Occurrence Frequency Matters

If the real question is not “does it exist” but “how many times does it appear,” use count().

python
1text = "banana"
2
3print(text.count("an"))
4print(text.count("na"))

This is still case-sensitive and substring-based, but it answers a different question from in.

Do not use count() > 0 just to simulate contains unless you truly need the count anyway. in is clearer for that purpose.

Case-Insensitive Checks

Python string membership is case-sensitive by default:

python
text = "Hello, World!"

print("world" in text)

That returns False.

If you want a case-insensitive check, normalize both sides first:

python
1text = "Hello, World!"
2needle = "world"
3
4print(needle.lower() in text.lower())

This is a common pattern for user input, search boxes, and simple filters.

Be careful with international text and locale-sensitive matching if your application needs more than basic case folding.

Substrings vs Whole Words

Another common source of confusion is that substring search does not care about word boundaries.

python
text = "catalog"

print("cat" in text)

That returns True, even though "cat" is not a separate word in the sentence. If you need whole-word matching, substring checks are not enough. At that point, tokenization or regular expressions may be more appropriate.

Why Python Uses in

Python leans heavily on operators that read like plain language. needle in haystack is short, expressive, and consistent with how membership works for lists, tuples, sets, and dictionaries.

So while there is no text.contains("world"), Python’s actual answer is arguably cleaner:

python
"world" in text

That is the idiom most Python developers expect to see.

Common Pitfalls

The most common pitfall is searching with find() and then writing extra logic just to convert the result into True or False. Use in instead when you only need membership.

Another mistake is forgetting that string checks are case-sensitive by default. "world" and "World" are different substrings.

A third issue is confusing substring checks with whole-word checks. "cat" in "catalog" is true, which may or may not match the intent.

Finally, some developers use index() when missing data is perfectly normal, then end up handling avoidable exceptions. In that case, find() or in is usually better.

Summary

  • Python does not have a separate string contains() method.
  • Use the in operator for the normal substring-presence check.
  • Use find() when you also need the starting position without raising an exception.
  • Use index() when a missing substring should be treated as an error.
  • Normalize with lower() when you need a simple case-insensitive search.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.