Skip to main content

How to Find a Substring in a String with Python

Tags

How to Find a Substring in a String with Python

If you have ever wondered how to find a substring inside another string in Python then hopefully this tutorial will help walk you through how to do that.  As an example, in Python 2.7 or above, if you have ever wondered how to determine if the substring "Agnosticdev," is part of the parent string "Hello Agnosticdev, I love Tutorials," then this tutorial will show you how to indicate, with a boolean value, the existence of the substring.  This tutorial takes advantage of Python's Standard Library API methods that are most commonly used by Python developers for their day to day development operations.  The Python Standard Library APIs contain a lot of useful information when working with string operations and before starting this tutorial it would be helpful to brush up on the difference between Python 2.7 and Python 3.6 before reading further.  

NOTE: This tutorial assumes that you are at least using Python 2.7 or greater.  The code samples in this tutorial will cover Python 2.7.13 and Python 3.6.1 and will be tested on Ubuntu (16.04) Linux and macOS Sierra 10.12.4.  I have not tested the code in a Window's environment, but if you are using a Linux subsystem on Windows with Python available, the results should be fairly similar, but cannot be guaranteed.

Code Example:

Python sub string

The most straightforward and widely available example of how to determine if a substring is available in a parent string is by performing a boolean check to determine if a parent string is containing a substring value.  This boolean check will return true if the substring is found and false if the substring is not found. Allowing your program to act upon that result accordingly.  This code example is available for use in Python 2.7 and Python 3.6 and is a very low-risk and computationally moderate way for checking the existence of a substring in Python.  If you can, I would take this approach in your program for sake of computation and simplicity.

string = "Hello Agnosticdev, I love Tutorials"
substring = "Agnosticdev"
 
# Straight forward approach for Python 2.7 and Python 3.6
# Executes the conditional statement when the substring is found
if substring in string:
	print ("Your substring was found!")

Alternative Approaches:

Python Alternative Approach Find Method

A very nice alternative approach to performing the if substring boolean check is to use the Python Standard Library string method for find().  This approach works very nicely because it is supported by Python 2.7 and Python 3.6.  The idea behind this approach is to determine the starting index of the substring you are looking for.  If you notice, I am performing a conditional check to determine if the result of the find method call is -1 or not.  If the substring is not found, the find method will return -1.  Meaning that the substring was not found in the parent string.  If the substring is found, the find method will return the starting index of the first character of the substring.  In this case the starting index is 6 because the first character of the substring is found at character 6 of the parent string.

# Alternative Approaches
string = "Hello Agnosticdev, I love Tutorials"
substring = "Agnosticdev"
 
 
# Use the find method in Python 2.7 or Python 3.6
if string.find(substring) is not -1:
	print("Python found the substring!")
else:
	print("Python did NOT find the substring!")

Now you know more about how to find substrings in parent strings in Python!  Please feel free to download the Python source example from my Github and mess around with it.  As always, if you have a comment or questions please feel free to reach out to me or leave a comment.

Thank you very much for reading!

Credits: Cover image designed by Freepik.

Member for

3 years 9 months
Matt Eaton

Long time mobile team lead with a love for network engineering, security, IoT, oss, writing, wireless, and mobile.  Avid runner and determined health nut living in the greater Chicagoland area.

Comments

hamada allaoui

Mon, 04/01/2019 - 07:23 AM

Hi
I'm looking for how to color a string of characetes in a Text widgte tkinter (python 3.6). at HTML,
we can use span tag :(I did not intentionally put < at the code below)
span class="mycolor" Hello, freinds your are welcome /span
style>
.mycolor {color: # FF0000; }
/style>

this sentence "Hello, freinds your are welcome" is written at each line with the color (# FF0000)
I want to use like this procedure in python 3.6 with string of characters

Anurag Sharma

Tue, 08/27/2019 - 05:14 PM

This .find() cant return the number of occurance of a string.
If we use .count() then it returns how many time a string is present.
But that also fail to return value if string in below case
String = ABCDCDC
SUBSTRING = CDC

avinash nukala

Tue, 10/08/2019 - 10:07 PM

these piece of code helped me a lot

Karan Modh

Sun, 11/10/2019 - 11:45 AM

How to get index of the last character of the string that is matched?