Find a string between two substrings in Python

In this python tutorial, we will learn how to find a string between two substrings in Python.

Table Of Contents

Find a string between two substrings using re.search()

The re.search() method available in the re module can be used to find the string present in between two substrings. It takes the pattern – (.*) in between two substrings. Finally, we will use group() to get the string.

Syntax:

re.search('substring1(.*)substring2', string).group(1)

Parameters:

Advertisements
  1. the First parameter takes a regex pattern to get the string between substring1 and substring2.
  2. the Second parameter takes the actual string.
  3. If match is found then group 1 will contain the string between ubstring1 and substring2.
  4. Get the value of group 1.

It will return the string between ubstring1 and substring2.

Example:

In this example, we will find the string in between the substrings – “Welcome” and “thisPointer”.

import re

# String
string1="Welcome to thisPointer"

print(string1)

subString1 = "Welcome"
subString2 = 'thisPointer'

# Find and return the substring between "Welcome" and "thisPointer"
matchedGroups = re.search(subString1 + '(.*)' + subString2, string1)

if matchedGroups and ( len(matchedGroups.groups()) > 0 ):
    subStr = matchedGroups.group(1)
    print("Substring between Welcome and thisPointer is - \"", subStr, "\"")
else:
    print('Given substrings does not exist')

Output:

Welcome to thisPointer
Substring between Welcome and thisPointer is -  to 

” to ” is the substring present in between the two substrings.

Find a string between two substrings using index() with for loop

We will iterate the string inside for loop with an iterator, The starting range is from substring1 and the ending range is substring2. We are adding the substring1 index (using the index() method) with the total length of the substring and specifying it as the start range. Finally, we are adding the string that exists between these two substrings to an empty string variable (subStr).

Syntax:

subStr = ""
for iterator in range(string.index(substring1) + len(substring1) + 1,string.index(substring2)):
    subStr = subStr + string[iterator]

Here,
1. substring1 is the first substring
2. substring2 is the second substring
3. iterator is used to iterate the string inside for loop

Example:

# String
string1="Welcome to thisPointer"

print(string1)

subStr = " "

# Find the substring between "Welcome" and "thisPointer"
for i in range(string1.index("Welcome") + len("Welcome") + 1, string1.index("thisPointer")):
    subStr = subStr + string1[i]

print("Substring between Welcome and thisPointer is - \"", subStr, "\"")

Output:

Welcome to thisPointer
Substring between Welcome and thisPointer is - "  to  "

” to ” is the substring present in between the two substrings.

Find a string between two substrings using find() with for loop

We will iterate the string inside for loop with an iterator. The starting range is from substring1 and the ending range is substring2. We are adding the substring1 index (using the find() method) with the total length of the substring and specifying the start range. Finally, we are adding the string that exists between these two substrings to an empty string variable (fisubStrnal).

Syntax:

subStr = ""
for iterator in range(string.find(substring1) + len(substring1) + 1,string.find(substring2)):
    subStr = subStr + string[iterator]

Here,
1. substring1 is the first substring
2. substring2 is the second substring
3. iterator is used to iterate the string inside for loop

Example:

# String
string1="Welcome to thisPointer"

print(string1)

subStr = " "

# Find the substring between "Welcome" and "thisPointer"
for i in range(string1.find("Welcome") + len("Welcome") + 1, string1.find("thisPointer")):
    subStr = subStr + string1[i]

print("Substring between Welcome and thisPointer is - \"", subStr, "\"")

Output:

Welcome to thisPointer
Substring between Welcome and thisPointer is - "  to  "

” to ” is the substring present in between the two substrings.

Find a string between two substrings using index() with slicing

Here, we will specify the substring1 as the first range and substring2 as the second range in the slice operator. Using the index() method, we will add the substring1 index with a total length of substring1 and specify it as the first range and we will specify the starting position of substring2 using the index() method as the last range.

Syntax:

string[string.index(substring1) + len(substring1 + 1: string.index(substring2)]

Where,
1. string is the actual string
2. substring1 is the first substring
3. substring2 is the second substring

It returns a substring between substring1 and substring2.

Example:

# String
string1="Welcome to thisPointer"

print(string1)

subString1 = "Welcome"
subString2 = "thisPointer"

# Find and return the substring between "Welcome" and "thisPointer"
subStr = string1[string1.index(subString1) + len(subString1) + 1: string1.index(subString2)]

print("Substring between Welcome and thisPointer is - \"", subStr, "\"")

Output:

Welcome to thisPointer
Substring between Welcome and thisPointer is - to 

The “to” is the substring present in between the two substrings.

Find a string between two substrings using find() with slicing

Here, we will specify the substring1 as the first range and substring2 as the second range in the slice operator. Using the find() method, we will add the substring1 index with a total length of substring1 and specify it as the first range and we will specify the starting position of substring2 using the find() method as the last range.

Syntax:

string[string.find(substring1) + len(substring1 + 1: string.find(substring2)]

Where,
1. string is the actual string
2. substring1 is the first substring
3. substring2 is the second substring

Example:

# String
string1="Welcome to thisPointer"

print(string1)

subString1 = "Welcome"
subString2 = "thisPointer"

# Find and return the substring between "Welcome" and "thisPointer"
subStr = string1[string1.find(subString1) + len(subString1) + 1: string1.find(subString2)]

print("Substring between Welcome and thisPointer is - \"", subStr, "\"")

Output:

Welcome to thisPointer
Substring between Welcome and thisPointer is - " to  "

The ” to ” is the substring present in between the two substrings.

Summary

We have seen how to find a substring between two substrings using index() and find() methods. Using these methods we discussed 2 scenarios – using for loop and slicing. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top