Getting substring in Python
In different programming languages, you might have seen functions to get the substrings from source strings. For example, in Java, the substring method is used to get the substring from the source string.
In Python, you may use a couple of ways for getting a substring from the source string. While there is no formal method like “substring” or “substr” etc. the techniques are called slicing and extended slicing.
A few quick examples with code
- An example of extended slicing
- Extended slicing with step parameter example
- With start position
- The example of Slicing object
The following section explains how to get Python substring by using these methodologies.
Substring in Python by extended slicing example
Basically, it is much wider than the ordinary “substring” methods as using the extended slicing. You may actually get parts of data or slice for sequences; not only just strings. Since, the string is also a sequence of characters in Python like lists, tuple, range etc.
As per the topic, we will only focus on string sequence and show you the examples of getting the substring by using extended slicing.
See the example below of getting a substring by using extended slicing:
See online demo and code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#A demo of substring by extended slice a_string = "Get Python String Substring" print("Original String:", a_string) substr = a_string[4:17] print("Substring:", substr) |

In the code, notice this line:
substr = a_string[4:17]
So, you have to specify the start and end position of the source string to get the substring. The complete syntax is as follows:
object[start:stop:step]
Where:
- object is the source string (can be any sequence like list etc.)
- Start – the character where you want to get a substring from
- Stop – specifies the end position in the source string
- Step – the default is 1
- Parameters are enclosed in the square brackets
- Parameters are separated by colon
The step parameter may be least required as using it with string, however, you may use this for any sequence including strings. See an example below.
An example of getting substring with step parameter
In the following example, the same string is used as in the above example, except I used the step parameter. See the code and output:
See online demo and code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#A demo of substring by extended slice with step parameter a_string = "Get Python String Substring" print("Original String:", a_string) substr = a_string[1:27:2] print("Substring with 2 steps:", substr) |

Only specifying the start position example
If you want to get a substring starting from a given start position that goes to the end of the string then just specify the start position. This is demonstrated in the example below:
See online demo and code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#A demo of substring by extended slice with step parameter a_string = "Hello World, Python is awesome" print("Original String:", a_string) substr = a_string[13:] print("Substring with 2 steps:", substr) |

An example of extracting substring from right of the string
You may also get the substring from the right of the string. For that, simply use a negative value for the parameters. See this example where I only provided the negative value for the start parameter:
See online demo and code
1 2 3 4 5 6 7 8 9 10 11 |
a_string = "Substring from right side" print("Original String:", a_string) substr = a_string[-13:] print("Substring with 2 steps:", substr) |
The output of the above code is:
Original String: Substring from right side
Substring with 2 steps: om right side
You can see, the substring is started from the right side.
2nd way – The slice object for getting the substring
There is a difference between the extended slice and creating a slicing object for getting a substring. You may read the details about both in the detailed tutorial (slicing and extended slice).
This is how a slicing object is created:
slice(start ,stop ,step)
The slice construct takes three indices; start, stop and step. These are separated by commas and enclosed in parenthesis.
The following example shows how to get a substring by using the slice object:
See online demo and code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#A demo substring by slice object slice_string = "The slice object to get substring" obj_slice = slice(3, 17, 1) print("Original string: " ,slice_string) print("Sliced substring with slice(3, 17, 1): " ,slice_string[obj_slice]) |

You may play with start and stop or only start index just like in extended slice examples. Besides, also try the negative values to get a substring from the right side.
You may read details and see examples in the tutorial here.