Summary: In this post, we will learn how to check if a string contains a substring in python.
String : "Let's code in Python" Substring : "Python" Output : True String : "Pencil Programmer" Substring : "Pen" Output : False
There are multiple ways to multiple ways to check if a Python string contains a substring. Let’s discuss each method with examples.
Method 1: Using
in operator returns true if a string contains another string as a substring. It is the easiest way to check for a substring in python.
Here is the python example using
in operator to check if string contains substring.
string = "Today is coding day" substring = "coding" if substring in string: print("Found") else: print("Not Found")
in operator functions similar to
__contains__ method, it can also be used to check for items in data structures like list.
Method 2: Using
find() method in Python can be used for checking if a string contains a substring.
It returns the index position of the first letter of the substring present in the string otherwise returns -1.
The general syntax for find method is
Here is an Python example using the find() method:
string = "Pencil Programmer" substring = "Pencil" if string.find(substring) != -1: print("Found") else: print("Not Found")
Method 3: Using
index() is another Python string class method that can be used to find the index of the first occurrence of a substring in the given string.
It is a little different from the find() method. If the substring is not present in the string then it throws a
ValueError, which needs to be handled using Python try and except.
The general syntax for index method is
Here is an Python example using the index() method:
string = "I love Programming" substring = "love" try: string.index(substring) except ValueError: print("Not Found") else: print("Found")
If you don’t want to handle any exception then use
find() method instead of
Method 4: Using
count() is another method in python of type string which returns the count of a substring in the string i.e. number of occurrences of a substring in the string.
It returns 0 if the substring is not found.
The syntax for the count method is
Python example using the count() method:
string = "This is a programming website" substring = "website" if string.count(substring) > 0: print("Found") else: print("Not Found")
count() method is useful for knowing the number of occurrences of the substring in the parent string.
Method 5: Using REGEX
REGEX (Regular Expression) provides a more flexible way to search for patterns or substring in the string.
In REGEX, We first define the pattern or substring as a string, and then we search the pattern in the parent string using
search() method comes as a part of a python builtin module called
re. So in order to use this method we have to import re module.
The syntax for the search() method is
from re import search string = "PencilProgrammer.com" substring = ".com" if search(substring, string): print("Found") else: print("Not found")
It is recommended to use REGEX when we intend to do complex operation on strings, as it is slower than all the methods which we have discussed in this post.
In this tutorial, we learned multiple ways to check if a string in python contains another string as a substring.