Back to Python
2026-02-285 min read

SUBSTR (Python Programming)

Learn SUBSTR (Python Programming) step by step with clear examples and exercises.

Title: Python SUBSTR Function Equivalent - A full guide

Why This Matters

In this tutorial, we'll explore the Python equivalent of the SUBSTR function through string slicing. Mastering string slicing is crucial for tackling real-world programming challenges, acing coding interviews, and even debugging pesky bugs in your code.

Prerequisites

To follow this tutorial, you should have a basic understanding of Python syntax, including variables, strings, and functions. If you're new to Python, we recommend reviewing our beginner-friendly Python lessons before diving into string slicing. Familiarize yourself with topics such as data types, operators, control structures, and functions.

Core Concept

The Python SUBSTR function is not explicitly available as a built-in function; however, we can achieve similar functionality using slicing in strings. Here's how it works:

my_string = "Hello, World!"
substring = my_string[start_index:end_index]

In the example above, my_string is our original string, and substring will hold the extracted substring. The indices start_index and end_index determine where to begin and end the extraction, respectively. Keep in mind that Python uses zero-based indexing, meaning the first character has an index of 0.

Using Negative Indices

You can also use negative indices to extract substrings from the end of a string. A negative index refers to the position from the end of the string, with -1 representing the last character, -2 the second-to-last, and so on. For example:

my_string = "Hello, World!"
substring = my_string[-5:] # Output: 'World!'

Extracting Substrings from Multiple Positions

You can also extract substrings from multiple positions by providing a list of indices as the start and end points. For example:

my_string = "Python is awesome!"
substring = my_string[::] # Output: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'a', 'w', 'e', 's', 'o', 'm', 'e', '!']

In this example, we extracted all characters from the original string using an empty slice (::), which means starting from index 0 and ending at the end of the string.

Worked Example

Let's work through a practical example using string slicing:

my_string = "Python is awesome!"
substring = my_string[6:12]
print(substring) # Output: 'awesome'

In this example, we extracted the substring "awesome" from the string "Python is awesome!" by specifying the indices 6 and 12 as the start and end points of our extraction, respectively.

Common Mistakes

  1. ### Forgetting to include the colon (:) in the slice notation:
substring = my_string[start_index start_index]
  1. ### Specifying an invalid index outside the string's range:
my_string = "Hello, World!"
substring = my_string[13:15] # IndexError: string index out of range (13 > 12)

Using a step in slicing:

my_string = "Hello, World!"
substring = my_string[::2] # Outputs: 'HloWrld' (skips every other character)

In the example above, we used a step of 2 in our slice notation, causing the function to skip every other character. If you want to extract every nth character from a string, use a step value of n.

Extracting Substrings with Overlapping Ranges:

my_string = "Python is awesome!"
substring1 = my_string[::] # Output: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'a', 'w', 'e', 's', 'o', 'm', 'e', '!']
substring2 = my_string[::2] # Output: 'Pythno awesom' (overlapping range)

In the example above, we extracted all characters from the original string using an empty slice (::) and then tried to extract every other character using a step of 2. However, this results in overlapping ranges, causing the output to be incorrect. To avoid this issue, you can either use separate variables for each substring or specify different start indices for each extraction.

Practice Questions

  1. Write a script to extract the first and last names from the following full name: 'John Doe'.
first_name, last_name = 'John Doe'.split(' ')
print(f"First Name: {first_name}")
print(f"Last Name: {last_name}")
  1. Write a script to find the second occurrence of the letter "a" in the following string: 'The quick brown fox jumps over the lazy dog and then the cat sat on the mat.'.
string = 'The quick brown fox jumps over the lazy dog and then the cat sat on the mat.'
index = string.find('a', string.find('a') + 1) # Use the find() method to search for the second occurrence
print(f"Second occurrence of 'a': {index}")
  1. Write a script to count the number of occurrences of the letter "e" in the following string: 'The quick brown fox jumps over the lazy dog.'.
string = 'The quick brown fox jumps over the lazy dog.'
count = 0
for char in string:
if char == 'e':
count += 1
print(f"Number of occurrences of 'e': {count}")

FAQ

Q: What happens when I use negative indices in Python string slicing?

A: Negative indices in Python string slicing allow you to extract substrings from the end of a string. A negative index refers to the position from the end of the string, with -1 representing the last character, -2 the second-to-last, and so on.

Q: How do I extract every nth character from a Python string using slicing?

A: To extract every nth character from a Python string, use a step value of n in your slice notation. For example, to extract every third character from the string "Hello, World!", you can use the following code:

my_string = "Hello, World!"
substring = my_string[::3] # Outputs: 'Hld' (every third character)

Q: What is the difference between an empty slice and a slice with a step value of 1 in Python?

A: An empty slice (::) in Python extracts all characters from the original string, starting from index 0 and ending at the end of the string. On the other hand, a slice with a step value of 1 ([start_index:end_index:1]) extracts only the characters at the specified indices, ignoring any characters in between. For example:

my_string = "Python is awesome!"
substring1 = my_string[::] # Output: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'a', 'w', 'e', 's', 'o', 'm', 'e', '!']
substring2 = my_string[::1] # Output: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'a', 'w', 'e', 's', 'o', 'm', 'e', '!']

In the example above, substring1 contains all characters from the original string, while substring2 only contains the characters at each index specified by the slice notation.

SUBSTR (Python Programming) | Python | XQA Learn