Python slice()

Filed Under: Python

Python slice() function returns a slice object representing the set of indices specified by range(start, stop, step).

Python slice()

Python slice function syntax is:


class slice(stop)
class slice(start, stop[, step])

Note that slice function returns slice object. We can pass this object as a set of indices with sequences such as string, list, tuple etc.

Python slice function allows us to create a stepwise sub-sequence easily without doing complete iteration on the existing sequence.

slice() function arguments

  • start: specifies the start of the index value. It’s optional and defaults to None.
  • stop: specifies the end of the index value. This is a mandatory parameter.
  • step: specifies the steps to take from start to stop index. It’s an optional parameter and defaults to None.

Python slice object has read-only data attributes – start, stop and step – which return the argument values (or default value).

Let’s see how to create slice objects.


s = slice(1, 10, 2)  # indexes 1,3,5,7,9
print(type(s))
print(s.start)
print(s.stop)
print(s.step)

s = slice(5)  # indexes 0,1,2,3,4
print(s.start)
print(s.stop)
print(s.step)

Output:



1
10
2
None
5
None

Python slice object has no use on its own, it’s useful when used in conjunction with sequences to create a sub-sequence.

Python slice string

Let’s see how to use slice function with string. We will pass slice object just like a normal index to get the substring value from a string.


s = slice(1, 10, 2)  # indexes 1,3,5,7,9
print('abcde'[s])

Output: bd

Note that if the slice indexes are more than the length of the sequence, no exception is raised and data is returned till the maximum available index.

We can also pass negative values for slice() function. In that case, the iteration will be performed backward i.e. from end to start.


s = slice(-1, -3, -1)  # indexes -1, -2
print('abcde'[s])

Output: ed

Python slice list/array

Let’s look at an example of using slice() function with list or array.


s = slice(0, 3)  # indexes 0, 1, 2
my_list = [1, 2, 3, 4, 5, 6]
print(my_list[s])

Output: [1, 2, 3]

Python slice tuple

We can use slicing with tuple too because it’s a sequence.


s = slice(2)
my_tuple = [1, 2, 3, 4, 5, 6]
print(my_tuple[s])

Output: [1, 2]

Python slice extended indexing syntax

Since slicing is very popular in numerical python, there is a shorthand way to create a slice object.


a[start:stop:step]

Let’s see some examples of slicing using the shorthand approach.


x = 'abcde'
y = x[1:3:1]  # 1,2
print(y)

y = x[1:3]  # 1,2
print(y)

y = x[2:]  # 2 to length of sequence
print(y)

y = x[:5:2]  # 0,2,4
print(y)

y = x[:]  # copy of sequence
print(y)

y = x[-1:-4:-1]  # reverse iteration, end to start
print(y)

Output:


bc
bc
cde
ace
abcde
edc

The output is self-explanatory and important details are already mentioned in the comments.

Summary

Python slice() is a very useful function. We can create sub-sequence based on steps, start and end indexes easily without doing complete iteration.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages