top of page
  • Writer's pictureHarini Mallawaarachchi

Python String: Check if String Contains a Substring

The manipulation of strings is one of the most common tasks in any programming language. Checking whether a string contains a substring helps to generalize conditionals and create more flexible code. In Python, there are many common ways to check if a string contains another string.

Let's take a look at how to check if a string contains substrings in Python.

The if ... in Operator

fullstring = "Python" 
substring = "on"

if substring in fullstring: # True
     print("Found!")
else:
     print("Not found!")

This is the easiest way to find if a substring is present in a string. When using the in operator in Python, data structures are checked to see if they are members. As a result, it returns a Boolean (True or False).



The String.index() Method

fullstring = "Python" 
substring = "th"
     
try:
     fullstring.index(substring) # 2
except ValueError:
     print("Not found!") 
else:
     print("Found!")

The index() method can be used to find the starting index of the first occurrence of a substring in a string.

If the substring is not found, a ValueError exception is thrown, which can be handled with a try-except-else block. This is useful when you need to know the position of the substring.



The String.find() Method

fullstring = "Python" 
substring = "th"
     
if fullstring.find(substring) != -1: # 2
     print("Not found!") 
else:
     print("Found!")

The String class has another method called find() which is more convenient to use than index(), mainly because we don't need to worry about handling any exceptions.

If find() doesn't find a match, it returns -1, otherwise, it returns the left-most index of the substring in the larger string.



Regular Expressions (RegEx) search()

from re import search

fullstring = "Python" 
substring = "th"

if search(substring, fullstring): # True
     print"Found!"
else:
     print"Not found!"

Regular expressions provide a more flexible way to check strings for pattern matching.

Python is shipped with a built-in module for regular expressions, called re. The re module contains a function called search(), which we can use to match a substring pattern:


If you are dealing with large search spaces or need a more complex matching function, this method is best. When it comes to simple substring matching, regex is too complex and slow., the complication and slower speed of regex should be avoided for simple substring matching use cases.



Summary

Method

When substring found

When substring is not found

​ if ... in Operator

returns True

returns False

String.index()

returns the leftmost index of the first substring occurrence.

ValueError exception is thrown

String.find()

returns the leftmost index of the first substring occurrence.

returns -1

search()

returns True

returns False

The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True /False.

Alternatively, by using the find() rather than index() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the substring without the need for exception handling.

REGEX is also an option, with search() generating a Match object if Python finds the first argument within the second one. However, this is considered too complex and slow.



Which method do you often use to check a Substring of a String?

  • 0%if ... in Operator

  • 0%String.index()

  • 0%String.find()

  • 0%search()


10 views0 comments

Recent Posts

See All

Comments


bottom of page