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:
B07X6C9RMFI have executed the above code and added the screenshot below.

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:
B07X6C9RMFI have executed the above code and added the screenshot below.

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:
B07X6C9RMFI have executed the above code and added the screenshot below.

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:
- How to Convert a String to Date in Python?
- How to Print Strings and Integers Together in Python?
- How to Truncate a String to a Specific Length in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.