How to Extract a Substring Between Two Characters in Python?

In this tutorial, I will explain how to extract a substring between two characters from a string in Python. As a Python developer based in the USA, I recently faced this issue while building a web scraper to extract product IDs from Amazon URLs as a part of my project for one of my clients. After researching and testing various methods, I found three effective ways to solve this problem. Let us learn more about these methods with examples.

Extract a Substring Between Two Characters in Python

Python provides several ways to extract a substring between two characters. Let us see some important methods.

Method 1. Use String Slicing and the index() Method

One simple approach to extracting a substring between two characters is to use string slicing along with the index() method. Here’s an example:

url = "https://www.amazon.com/dp/B07X6C9RMF"
start_char = "/dp/"
end_char = "?"

# Find the start index
start_index = url.index(start_char) + len(start_char)

# Check if the end_char exists
if end_char in url[start_index:]:
    # If end_char exists, find the end index
    end_index = url.index(end_char, start_index)
    product_id = url[start_index:end_index]
else:
    # If end_char doesn't exist, extract till the end of the string
    product_id = url[start_index:]

print(product_id)

Output:

B07X6C9RMF

I have executed the above code and added the screenshot below.

Extract a Substring Between Two Characters in Python

In this example, we have a URL string and want to extract the product ID between the / and ? characters. We first find the index of the starting character index() and add its length to get the starting index of the substring. Then, we find the index of the ending character, starting from the previously found index. Finally, we use slice notation to extract the substring between these two indices.

Read How to Fix Unterminated String Literals in Python?

Method 2. Use Regular Expressions

Another powerful method to extract a substring between two characters is to use regular expressions and the re module in Python. Regular expressions provide a more flexible and robust way to search for patterns in strings. Here’s an example:

import re

url = "https://www.amazon.com/dp/B07X6C9RMF"
pattern = r"/dp/([^/?]+)"  # Updated pattern

match = re.search(pattern, url)
if match:
    product_id = match.group(1)
    print(product_id)
else:
    print("Product ID not found")

Output:

B07X6C9RMF

I have executed the above code and added the screenshot below.

How to Extract a Substring Between Two Characters in Python

In this example, we define a regular expression pattern that matches the substring between the /dp/ and either a / or ? character. The (.+?) part captures the substring we want to extract. We use the re.search() function to find the first match of the pattern in the URL string. If a match is found, we retrieve the captured substring using match.group(1).

Check out How to Use Python Triple Quotes with F-Strings for Multiline Strings?

Method 3. Use the split() Method

Another approach is to use the split() method to split the string based on specific characters and then extract the desired substring. Here’s an example:

url = "https://www.amazon.com/dp/B07X6C9RMF"
parts = url.split("/dp/")
if len(parts) > 1:
    product_id = parts[1].split("/")[0]
    print(product_id) 
else:
    print("Product ID not found")

Output:

B07X6C9RMF

I have executed the above code and added the screenshot below.

Extract a Substring Between Two Characters in Python split()

In this example, we split the URL string using the /dp/ substring. If the resulting list has more than one element, we know the product ID is present. We then split the second part of the list using the / character and take the first element, which is the product ID.

Read How to Do Case-Insensitive String Comparisons in Python?

Conclusion

In this tutorial, I explain how to extract a substring between two characters from a string in Python. I explained various methods, such as string slicing with index(), regular expressions with the re module, or the split() method.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.