Hit enter after type your search item

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

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


substring

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.

The slicing is a Python methodology that enables accessing parts of data from the given sequence like lists, tuples, strings etc. and objects that support sequence protocol. Read slicing in detail with examples

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


substring step

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


substring start

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


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


substring 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.

This div height required for enabling the sticky sidebar