Skip to main content
Python

Check if String Contains a Substring in Python

In this Python tutorial, you'll learn various methods to check for substring inside a string with proper code examples.

Ankush Das

Warp Terminal

Python has several methods to deal with strings. In this tutorial, I’ll show you how to know if a string contains a substring.

How to check if a string contains a substring

No matter whether it’s just a word, a letter or a phrase that you want to check in a string, with Python you can easily utilize the built-in methods and the membership test in operator.

It is worth noting that you will get a boolean value (True or False) or an integer to indicate if the string contains what you searched for. You’ll know more about it as I show the code below.

Let us take a look at the potential solutions with the help of which you can find if a string contains a specific word/letter.

Method 1. Using ‘in’ operator

Suppose you have a string “str” already defined, here’s how it will look like:

stringexample = "terminator"
"ter" in str

The output for this will be “True” because the string contains what I searched for. So, if you search for something and it does not exist in the string, you will get “False” as the output.

If you’re still not sure, let me write a small program to explain the same:

stringexample = "Terminator"
substring = "ter"

if substring in stringexample:
    print ("We've found the string!")
else:
    print ("Oops, not found!")

The output for the above code will be:

We've found the string!

Method 2: Using Find Method

If you want to use a method to find a substring, you can use the find() method. Here’s how to do that:

stringexample = "kiki"
stringexample.find("ki")

The output for this will be 0.

The reason behind the output is – this method returns the lowest index value when the substring is found.

If it returns -1 that means the string does not contain that substring.

Here’s a small program to show it in action:

stringexample = "Kiki"
substring = "ki"

if stringexample.find("ki") != -1:
    print ("We've found the string!")
else:
    print ("Oops, not found!")

The output for the above code will be:

We've found the string!

Method 3: Using Contains Method

__contains__() is another function to help you to check whether a string contains a particular letter/word.

Here’s how you can use it:

stringexample = "kiki"
stringexample.__contains__("k")

You will get the output as True/False. For the above code snippet, you will get the output as:

True

Do note that there are 4 underscore symbols involved when you want to write the method (2 before the word and 2 after).

Here’s a program to explain the same:

stringexample = "kiki"
if stringexample.__contains__("k") == True:
  print ("Yeyy, found the substring!")
else:
  print ("Oops, not found!")

In this case, the output is:

Yeyy, found the substring!

Wrapping Up

If you find to utilize a search algorithm from scratch without using the functions or the above-mentioned solutions, you can take a look at some of the string search algorithms to explore more about them.

If you have questions or suggestions, please let me know.

Ankush Das