How to extract a substring from inside a string in Python?

In programming, a substring is a smaller part of a larger string. Extracting substrings is a common task in Python, whether you're analyzing user input, manipulating URLs, or processing text data.

Python provides several methods to extract substrings from strings. Let's explore the most effective approaches.

Using String Slicing

The most common approach is string slicing, which uses start and end indices to extract a portion of the string.

Syntax

string[start:end]

Example

Here we extract the substring "TP" from the string "Welcome To TP" ?

text = "Welcome To TP"
result = text[11:13]
print(result)

The output of the above code is ?

TP

Using the find() Method

The find() method returns the index where a substring first occurs. It returns -1 if the substring is not found.

Syntax

string.find(substring, start, end)

Example

Let's extract "Vanakam" from the string using find() with slicing ?

text = "Hi, Hello, Vanakam"
start_index = text.find("Vanakam")
if start_index != -1:
    result = text[start_index:start_index + len("Vanakam")]
    print(result)
else:
    print("Substring not found")

The output of the above code is ?

Vanakam

Using Regular Expressions

The re module provides powerful pattern matching capabilities for complex substring extraction using regular expressions.

Example

Here we extract a price pattern from a string using re.search() ?

import re

text = 'The phone is priced at $15,745.95 and has a camera.'
pattern = r'(\$[0-9,]*\.[0-9]{2})'
match = re.search(pattern, text)

if match:
    print("Price found:", match.group(1))
else:
    print("No price pattern found")

The output of the above code is ?

Price found: $15,745.95

Using partition() Method

The partition() method splits a string into three parts: before separator, separator, and after separator ?

email = "user@example.com"
username, separator, domain = email.partition("@")
print("Username:", username)
print("Domain:", domain)

The output of the above code is ?

Username: user
Domain: example.com

Comparison of Methods

Method Best For Returns
Slicing [start:end] Known positions Substring
find() Finding position first Index or -1
Regular expressions Pattern matching Match object
partition() Splitting by delimiter Three-part tuple

Conclusion

Use slicing for simple extraction with known positions, find() for locating substrings, and regular expressions for complex pattern matching. Choose partition() when splitting strings by delimiters.

Updated on: 2026-03-24T16:44:43+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements