Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Python Substring - How to Substring a String in Python?

Python Substring - How to Substring a String in Python?

Author image

Mayank Jain

Software Developer

Published on Fri Feb 16 2024

A Python substring refers to a portion of a string. A Python substring is a contiguous sequence of characters within the original string. Substrings can be extracted based on their position within the string, as shown in the following example using indexing or slicing operations.

Indexing allows access to individual characters within a string, while slicing enables the extraction of consecutive characters, a range of characters, or the entire string, forming a substring.

For example, if we have a string "hello" and want to extract the substring "ell", we can do so by slicing the string from index 1 to index 4 (string[1:4]). This will return the substring "ell", starting from the second character ('e') up to the end index of the fourth character ('o').

Python's substring functionality provides flexibility in manipulating strings, allowing developers to efficiently work with specific segments of text data within a larger string.

How to Substring a String in Python

Using String Slicing

One of the most common methods to extract substrings from a string in Python is through string slicing. String slicing allows you to obtain a portion of a string by specifying a range of indices.

Here's a simple example demonstrating how to use string slicing to extract a substring from a string variable:

# Define a string
original_string = "Hello, world!"

# Extract a substring using string slicing
substring = original_string[7:12]


# Print the substring
print(substring)

Output:

world

In this example, we have a string "Hello, world!". By specifying the range [7:12], we extract the substring "world", which consists of characters from index 7 (inclusive) to index 11 (exclusive).

Substring a String in Python Using str.split() Function

Another approach to extract substrings from a string in Python involves using the str.split() function. This method splits a string into a list of substrings based on a specified separator.

Here's an example demonstrating how to use str.split() to extract a substring:

# Define a string
original_string = "The quick brown fox jumps over the lazy dog"

# Split the string by whitespace
words = original_string.split()


# Extract a substring
substring = words[2]

# Print the substring
print(substring)

Output:

brown

In this example, the original string "The quick brown fox jumps over the lazy dog" is split into a list of words based on whitespace. We then extract the third word from the list, which corresponds to the substring "brown".

Substring a String in Python Using Regular Expressions

Regular expressions provide a powerful way to extract substrings from a given string in Python based on specific patterns or criteria. This allows for more complex substring extraction compared to simple string slicing or splitting.

Here's an example demonstrating how to use regular expressions to extract a substring:

import re

# Define a string
original_string = "The quick brown fox jumps over the lazy dog"

# Define a regular expression pattern to match a word starting with "brown"
pattern = r'\bbrown\b'

# Use re.search() to find the first occurrence of the pattern in the string
match = re.search(pattern, original_string)

# Extract the matched substring
substring = match.group()

# Print the substring
print(substring)

Output:

brown

In this example, we use a regular expression pattern \bbrown\b to match the word "brown" as a whole word within the string. We then use re.search() to find the first occurrence of this pattern in the original string. Finally, we extract and print the matched substring, which is "brown".

Python offers various methods to extract substrings efficiently. Techniques like string slicing, the str.split() function, and regular expressions cater to different substring extraction needs. String slicing provides simplicity and speed for basic extractions. The str.split() function offers flexibility for substring extraction based on delimiters. Regular expressions empower complex pattern-based substring extraction. Understanding these methods equips Python developers with versatile tools for substring manipulation tasks.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.