How to Check if a String Contains a Substring in Python
If you want to check whether one piece of text appears inside another piece of text in Python, there are a few simple ways to do it.
For beginners, the best option is usually the in operator. It is easy to read, easy to write, and works well when you only need a yes-or-no answer.
This page shows:
- how to check if a smaller string exists inside a larger string
- the simplest beginner-friendly method first
- how case-sensitive matching works
- when to use
in,find(), andindex()
Quick answer
text = "Hello, world!"
substring = "world"
if substring in text:
print("Found")
else:
print("Not found")
Output:
Found
For most cases, use the in operator. It is the simplest and most readable option.
What this page helps you do
- Check whether a smaller string exists inside a larger string
- Use the simplest beginner-friendly method first
- Understand case-sensitive matching
- Choose between
in,find(), andindex()
Use the in operator
The in operator is the best choice for a simple substring check.
It returns:
Trueif the substring existsFalseif it does not exist
It reads almost like plain English:
substring in text
Example:
text = "Python is fun"
substring = "is"
print(substring in text)
Output:
True
Another example:
text = "Python is fun"
substring = "java"
print(substring in text)
Output:
False
Use this approach when you only need to know whether the text is present.
Case sensitivity matters
String matching in Python is case-sensitive.
That means uppercase and lowercase letters are treated as different characters.
For example, "Hello" and "hello" are not the same:
text = "Hello, world!"
substring = "hello"
print(substring in text)
Output:
False
Even though the words look similar, Python does not treat them as a match because the first letter has different casing.
If you want to ignore case, convert both strings before checking. The most common choice is lower().
text = "Hello, world!"
substring = "hello"
print(substring.lower() in text.lower())
Output:
True
Use find() when you need the position
Use find() when you want to know where the substring starts.
find() returns:
- the starting index of the first match
-1if the substring is not found
Example:
text = "Hello, world!"
substring = "world"
position = text.find(substring)
print(position)
Output:
7
The value 7 means the substring starts at index 7.
If the substring does not exist:
text = "Hello, world!"
substring = "Python"
position = text.find(substring)
print(position)
Output:
-1
This is useful when you need both:
- a check for existence
- the position of the match
Use index() only if you expect a match
index() is similar to find().
It also returns the starting position of the substring:
text = "Hello, world!"
substring = "world"
position = text.index(substring)
print(position)
Output:
7
But there is one important difference:
find()returns-1if the substring is missingindex()raises an error if the substring is missing
Example:
text = "Hello, world!"
substring = "Python"
position = text.index(substring)
print(position)
This causes:
ValueError: substring not found
Because of that, index() is usually not the best first choice for beginners when the substring may not exist.
If you want to learn more about this error, see ValueError: substring not found.
How to do a case-insensitive check
If you want "Hello" and "hello" to match, convert both strings to the same case before checking.
A common approach is lower():
text = "Hello, World!"
substring = "world"
if substring.lower() in text.lower():
print("Found")
else:
print("Not found")
Output:
Found
You can also use casefold(), which is often better for more complete case-insensitive matching:
text = "Hello, World!"
substring = "WORLD"
print(substring.casefold() in text.casefold())
Output:
True
A good habit is to check the converted values, but keep the original text unchanged if you still need it later.
Common beginner mistakes
Here are some common problems when checking for a substring:
- Using
=instead of==orin - Forgetting that matching is case-sensitive
- Using
index()without handling missing text - Checking a non-string value without converting it first
Mistake: using = instead of in
This is wrong:
text = "Hello, world!"
substring = "world"
if substring = text:
print("Found")
= is for assignment, not comparison or membership checking.
Use in instead:
text = "Hello, world!"
substring = "world"
if substring in text:
print("Found")
Mistake: checking values that are not strings
If one of your values is not a string, convert it first.
text = "Order number: 12345"
number = 12345
print(str(number) in text)
Output:
True
If you do not convert the number, Python will raise a type-related error.
Mistake: extra spaces
Sometimes the text looks correct, but it contains spaces you did not notice.
text = "Hello, world! "
substring = "world!"
print(substring in text)
This still works, but hidden spaces often cause confusing results in other situations.
When debugging, repr() helps you see the exact string contents:
text = "Hello, world! "
print(repr(text))
Output:
'Hello, world! '
Debugging checks that do not work
If your code gives an unexpected result, these checks can help:
print(text)
print(substring)
print(repr(text))
print(repr(substring))
print(type(text))
print(type(substring))
print(substring in text)
print(text.find(substring))
These are useful when:
- the substring exists but uses different letter casing
- extra spaces make the text different than expected
- the variable is not a string
index()is used when the substring may not exist
FAQ
What is the easiest way to check for a substring in Python?
Use the in operator. It is the simplest and most readable choice for a yes or no check.
Is substring matching case-sensitive in Python?
Yes. Uppercase and lowercase letters are treated as different unless you convert both strings first.
What is the difference between in and find()?
in returns True or False. find() returns the position of the match or -1 if not found.
Why does index() give an error?
index() raises a ValueError when the substring is not found.
How do I ignore uppercase and lowercase when checking?
Convert both strings with lower() or casefold(), then compare those values.