Skip to Content

Check if a String contains a Substring in Python

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.

Check if string contains 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:

False

Here we get False as the output.

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.

There are other methods as well that you can use to check whether a string contains another string or not in Python.

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.

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.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.