Python Slice Function

The Python slice() function allows us to slice a sequence. In plain English, this means we can retrieve a part of a string, tuple, list, etc. We can specify the start, end, and step of the slice. The step lets you skip items in the sequence.

This is the syntax:

  • [:]: Items from the entire sequence.
  • [start:]: Items from start until the end of the sequence.
  • [:stop]: Items from the beginning until stop.
  • [start:stop]: Items from start until stop.
  • [start:stop:step]: Items from start until stop and skip items by step.

We specify the start, stop, and end with an integer. We can use positive and negative integers. This is best explained with some examples.

String Slicing

Consider the following string:

 +---+---+---+---+---+---+---+
 | G | i | g | a | b | i | t | 
 +---+---+---+---+---+---+---+
 0   1   2   3   4   5   6   7
-7  -6  -5  -4  -3  -2  -1

We use the integers above as indices. You can see them as a pointer between the characters. For example:

  • Integer 3 returns “a”.
  • Integer -2 returns “i”.
As you can see above, positive integers start with 0 but negative integers start with -1.

Entire Sequence

This example doesn’t have any value, but it is valid Python:

This prints the entire string.

Start

Let’s slice and start at index 4:

Or slice and start with a negative index:

Stop

We can also slice from the beginning, until the stop:

Or with a negative index:

Start Stop

Let’s slice with a start and stop indices:

We can also do this with negative indices:

Start Stop Step

How about skipping some items? We can include a start, stop, and step:

Every other character is now skipped. We can also do this with negative indices:

That’s how we slice a string.

Instead of using [], you can also use slice(). For example: "Gigabit"[0:4] is the same as "Gigabit"[slice(0,4)]. The [] notation looks cleaner to me.

List Slicing

Let’s try slicing a list. Here is an example with the indices:

 +----+----+----+----+----+----+
 | L0 | L1 | L2 | L3 | L4 | L5 |
 +----+----+----+----+----+----+
 0    1    2    3    4    5    6
-6   -5   -4   -3   -2   -1

Entire Sequence

Let’s slice the entire sequence:

I can’t think of any valid reason why you might want to use this, but for the sake of completion, here it is.

Start

Let’s slice with a start and a positive integer:

Or slice with a negative integer:

Stop

We can also start from the beginning and specify a stop:

Or use a negative integer:

Start Stop

With start stop, we can select a range:

You can also do this with negative indices:

Start Stop Step

How about skipping some items with a step?

Which is also possible with negative indices:

That’s all there is to it.

Real Life Examples

You might wonder where you would use slicing. How about I connect to an ASA firewall, retrieve some output, and extract some information?

Let’s say we receive the following string from the show version command:

asa_hardware = "ASA5506, 4096 MB RAM, CPU Atom C2000 series 1250 MHz, 1 CPU (4 cores)"

I can use slicing to extract the information I want. Here is an example:

The example above works, but there is one downside to slicing. The string you slice always has to be the same. Slicing only returns the characters you selected with the indices and doesn’t look at the text. When the input string is slightly different, you’ll get a different result. For example, imagine we use the slicing above on another ASA firewall, perhaps the 5515-X. The “show version” string would be slightly different, and we don’t get the values we were looking for. In a scenario where your strings might change, it’s better to use regex.


Ask a question or start a discussion by visiting our Community Forum