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 to get 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.
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 while 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, tuples, range,s etc.
As per the topic, we will only focus on string sequence and show you examples of getting the substring by using extended slicing.
See the example below of getting a substring by using extended slicing:
#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:
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||The source string (can be any sequence like a list, etc.)|
|Start||The character position from which you want to get a substring|
|Stop||Specifies the ending position in the source string|
|Step||The default step is 1|
- Parameters are enclosed in the square brackets
- Parameters are separated by a colon
The step parameter may be least required as using it with string, however, you may use this for any sequence including strings. See the 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:
#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)
Substring with 2 steps: e yhnSrn usrn
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:
#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)
Substring with 2 steps: Python is awesome
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:
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:
The slice construct takes three indices; start, stop, and step. These are separated by commas and enclosed in parentheses.
The following example shows how to get a substring by using the slice object:
#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])
Sliced substring with slice(3, 17, 1): slice object
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 the details and see examples in the tutorial here.