Skip to content
Related Articles

Related Articles

Python – Extract string between two substrings

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 13 Oct, 2022
Improve Article
Save Article

Given a string and two substrings, write a Python program to extract the string between the found two substrings. 

Example:

Input : test_str = “Gfg is best for geeks and CS”, sub1 = “is”, sub2 = “and” 

Output: best for geeks

Explanation: best for geeks is between is and ‘and’

Input : test_str = “Gfg is best for geeks and CS”, sub1 = “for”, sub2 = “and” 

Output: geeks

Explanation: geeks is between for and ‘and’

Using index() + loop to extract string between two substrings

In this, we get the indices of both the substrings using index(), then a loop is used to iterate within the index to find the required string between them.

Python3




# Python3 code to demonstrate working
# of Extract string between 2 substrings
# Using loop + index()
 
# initializing string
test_str = "Gfg is best for geeks and CS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing substrings
sub1 = "is"
sub2 = "and"
 
# getting index of substrings
idx1 = test_str.index(sub1)
idx2 = test_str.index(sub2)
 
res = ''
# getting elements in between
for idx in range(idx1 + len(sub1) + 1, idx2):
    res = res + test_str[idx]
 
# printing result
print("The extracted string : " + res)


Output:

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Using index() + string slicing to extract string between two substrings

Similar to the above method, just the task of slicing is performed using string slicing for providing a much compact solution. 

Python3




# Python3 code to demonstrate working
# of Extract string between 2 substrings
# Using index() + string slicing
 
# initializing string
test_str = "Gfg is best for geeks and CS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing substrings
sub1 = "is"
sub2 = "and"
 
# getting index of substrings
idx1 = test_str.index(sub1)
idx2 = test_str.index(sub2)
 
# length of substring 1 is added to
# get string from next character
res = test_str[idx1 + len(sub1) + 1: idx2]
 
# printing result
print("The extracted string : " + res)


Output:

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Using find()+ string slicing to extract string between two substrings

find() method returns the position of string passed as argument or else returns -1.Finally slice the string with the positions.

Python3




# Python3 code to demonstrate working
# of Extract string between 2 substrings
# Using find() + string slicing
 
# initializing string
test_str = "Gfg is best for geeks and CS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing substrings
sub1 = "is"
sub2 = "and"
 
# getting index of substrings
idx1 = test_str.find(sub1)
idx2 = test_str.find(sub2)
 
# length of substring 1 is added to
# get string from next character
res = test_str[idx1 + len(sub1) + 1: idx2]
 
# printing result
print("The extracted string : " + res)


Output:

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Using replace() and split() to extract string between two substrings

Here we are using the replace and splitting the original string till we get the desired substrings at the corner and then extract it.

Python3




# Python3 code to demonstrate working
# of Extract string between 2 substrings
 
# initializing string
test_str = "Gfg is best for geeks and CS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing substrings
sub1 = "is"
sub2 = "and"
 
test_str=test_str.replace(sub1,"*")
test_str=test_str.replace(sub2,"*")
re=test_str.split("*")
res=re[1]
 
# printing result
print("The extracted string : " + res)


Output:

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Using regex to extract string between two substrings

Here we are using a built-in library i.e. we are using the regex to extract string between two substrings.

Python3




import re
test_str = "Gfg is best for geeks and CS"
 
# printing original string
print("The original string is : " +
      str(test_str))
 
# initializing substrings
sub1 = "is"
sub2 = "and"
 
s=str(re.escape(sub1))
 
e=str(re.escape(sub2))
 
# printing result
res=re.findall(s+"(.*)"+e,test_str)[0]
 
print("The extracted string : " + res)


Output:

The original string is : Gfg is best for geeks and CS
The extracted string :  best for geeks 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!