Skip to content
Home / Fundamentals

How to Get a Substring of a String

Method 1: Slicing

In Python, you can extract a substring from a string using the slicing notation. The slicing notation consists of a start index, a stop index, and a step, separated by colons.

Here is an example of how to extract a substring from a string using slicing:

# Get the substring 'Python' from the string 'Learn Python'
string = 'Learn Python'
substring = string[6:12]
print(substring)
Python
In this example, the start index is 6, the stop index is 12, and the step is 1. The start index is the index of the first character in the substring, and the stop index is the index of the character after the last character in the substring.

You can also use negative indices to slice a string. Negative indices count from the end of the string, with -1 being the index of the last character.

Here is an example of using negative indices to slice a string:

```python
# Get the substring 'Python' from the string 'Learn Python'
string = 'Learn Python'
substring = string[-7:-1]
print(substring)
Python

You can also specify a step value to slice the string. The step value determines the number of characters to skip between indices. A step value of 2 would skip every other character, a step value of 3 would skip two characters and select the third, and so on.

Here is an example of using a step value to slice a string:

# Get every other character from the string 'abcdefg'
string = 'abcdefg'
substring = string[::2]
print(substring)
aceg

You can also combine start and stop indices with a step value to extract a substring from a specific range of indices.

Here is an example of combining start and stop indices with a step value to slice a string:

# Get every other character from the string 'abcdefg' starting at index 1
string = 'abcdefg'
substring = string[1::2]
print(substring)
bdf

Method 2: str.slice()

You can also use the str.slice() method to extract a substring from a string. This method is similar to the slicing notation, but it uses named parameters instead of indices.

Here is an example of using the str.slice() method to extract a substring from a string:

# Get the substring 'Python' from the string 'Learn Python'
string = 'Learn Python'
substring = string.slice(start=6, stop=12)
print(substring)
Python

Method 3: str.substring()

You can also use the str.substring() method to extract a substring from a string. This method is similar to the str.slice() method, but it uses different parameter names and is not available in Python.

Here is an example of using the str.substring() method to extract a substring from a string:

# Get the substring 'Python' from the string 'Learn Python'
string = 'Learn Python'
substring = string.substring(beginIndex=6, endIndex=12)
print(substring)
Python

In this example, the beginIndex parameter is the index of the first character in the substring, and the endIndex parameter is the index of the character after the last character in the substring.

You can also use the str.substring() method to extract a substring from a specific range of indices by specifying only the beginIndex parameter.

Here is an example of using the str.substring() method to extract a substring from a specific range of indices:

# Get the substring 'def' from the string 'abcdefg'
string = 'abcdefg'
substring = string.substring(beginIndex=3)
print(substring)
defg

In this example, the beginIndex parameter is 3, so the substring starts at the third character in the string and includes all characters after that.

Method 4: str.split()

You can also use the str.split() method to extract a substring from a string. This method splits a string into a list of substrings based on a specified delimiter, and you can then select a specific element from the list to use as the substring.

Here is an example of using the str.split() method to extract a substring from a string:

# Get the second word from the string 'Learn Python'
string = 'Learn Python'
substrings = string.split()
substring = substrings[1]
print(substring)
Python

In this example, the string is split into a list of substrings using a single space as the delimiter. The second element in the list is then selected as the substring.

Method 4: str.find()

You can also use the str.find() method to extract a substring from a string. This method searches for a specified substring within a string and returns the index of the first occurrence. You can then use this index to slice the string and extract the substring.

Here is an example of using the str.find() method to extract a substring from a string:

# Get the substring 'Python' from the string 'Learn Python'
string = 'Learn Python'
index = string.find('Python')
substring = string[index:index+6]
print(substring)
Python

In this example, the str.find() method is used to find the index of the first occurrence of the substring 'Python' in the string. The index is then used to slice the string and extract the substring.