In this tutorial, we will look at how to check whether a string contains a given substring or not in Python with the help of some examples.
How to check if a string contains a substring in Python?
The easiest way to check whether a string contains a substring or not in Python is to use the membership operator in
. The following is the syntax:
# sub is the substring and s is the main string sub in s
It returns True
if the substring is a part of the string and False
otherwise. Note that this operation is case-sensitive.
Let’s look at an example –
# string and substring s = "you have a metal arm!" sub = "metal" # check substring in string print(sub in s)
Output:
True
We get True
as the output, since here the string “you have a metal arm!” contains the substring “metal”.
Note that the above operation is case-sensitive. For example –
# string and substring s = "you have a metal arm!" sub = "Metal" # check substring in string print(sub in s)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
False
Here we get False
as the output.
Check if string contains substring irrespective of the case
To check for the presence of a substring in a string, irrespective of the case, transform both the string and the substring to the same case (either lowercase or uppercase) and then use the in
operator.
# string and substring s = "you have a metal arm!" sub = "Metal" # check substring in string print(sub.lower() in s.lower())
Output:
True
Now, we get True
as the output.
Other methods to check for substring in string
There are other methods as well that you can use to check whether a string contains another string or not in Python.
Using string index()
function
Use the string index()
function to get the index of the substring inside the string.
If the substring is present in the string, it will return the starting index of the substring in the string. If the substring is not present, it will raise a ValueError
.
# string and substring s = "you have a metal arm!" sub = "metal" # check substring in string try: i = s.index(sub) print("'{}' present in '{}' at index: {}".format(sub, s, i)) except ValueError: print("Not present")
Output:
'metal' present in 'you have a metal arm!' at index: 11
We get the starting index of the substring “metal” in the string “you have a metal arm!” as the return value from the function, indicating that the substring is in fact present in the string.
Note that, this method is also case-sensitive. Convert both the strings to the same case before using this method if you want to check for the substring in the string irrespective of the case.
Using string find()
function
The string find()
function works similarly to the string index()
function but it doesn’t raise any errors if the character (or, the substring in our case) is not present in the string.
It returns -1
if the character is not present in the string.
# string and substring s = "you have a metal arm!" sub = "metal" # check substring in string i = s.find(sub) if i == -1: print("Not present") else: print("'{}' present in '{}' at index: {}".format(sub, s, i))
Output:
'metal' present in 'you have a metal arm!' at index: 11
We get the starting index of the substring, sub in the string, s as the return value from the function. Now, since this index is not -1, we can confirm that the substring is present in the string.
Again, this method is also case-sensitive. Convert both the strings to the same case before using this method if you want to check for the substring in the string irrespective of the case.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.