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:

>>> "Gigabit"[:]

Will show this output:

'Gigabit'

This prints the entire string.

Start

Let’s slice and start at index 4:

>>> "Gigabit"[4:]

Will show this output:

'bit'

Or slice and start with a negative index:

>>> "Gigabit"[-3:]

Will show this output:

'bit'

Stop

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

>>> "Gigabit"[:4]

Will show this output:

'Giga'

Or with a negative index:

>>> "Gigabit"[:-3]

Will show this output:

'Giga'

Start Stop

Let’s slice with a start and stop indices:

>>> "Gigabit"[3:5]

Will show this output:

'ab'

We can also do this with negative indices:

>>> "Gigabit"[-6:-3]

Will show this output:

'iga'

Start Stop Step

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

>>> "Gigabit"[0:7:2]

Will show this output:

'Ggbt'

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

>>> "Gigabit"[-6:-1:2]

Will show this output:

'iai'

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:

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[:]

Will show this output:

['L0', 'L1', 'L2', 'L3', 'L4', 'L5']

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:

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[3:]

Will show this output:

['L3', 'L4', 'L5']

Or slice with a negative integer:

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[-2:]

Will show this output:

['L4', 'L5']

Stop

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

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[:3]

Will show this output:

['L0', 'L1', 'L2']

Or use a negative integer:

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[:-4]

Will show this output:

['L0', 'L1']

Start Stop

With start stop, we can select a range:

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[3:5]

Will show this output:

['L3', 'L4']

You can also do this with negative indices:

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[:-4:-1]

Will show this output:

['L5', 'L4', 'L3']

Start Stop Step

How about skipping some items with a step?

>>> interfaces = ["L0","L1","L2","L3","L4","L5"]

>>> interfaces[1:4:2]

Will show this output:

['L1', 'L3']

Which is also possible with negative indices:

Unlock This Lesson for Free - No Credit Card Needed!

If you like to keep on reading, register now!

  • Learn CCNA, CCNP and CCIE R&S. Explained As Simple As Possible.
  • Get Instant Access to this Full Lesson, Completely for Free!
  • Unlock More to Read. More Lessons Added Every Week!
  • Content created by Rene Molenaar (CCIE #41726)
2409 Sign Ups in the last 30 days

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