Python slice()

The slice() constructor creates a slice object representing the set of indices specified by range(start, stop, step).

The slice object is used to slice a given sequence (string, bytes, tuple, list or range) or any object which supports sequence protocol (implements __getitem__() and __len__() method).

Slice object represents the indices specified by range(start, stop, step).

The syntax of slice() are:

slice(stop)
slice(start, stop, step)

slice() Parameters

slice() mainly takes three parameters which have the same meaning in both constructs:

  • start - starting integer where the slicing of the object starts
  • stop - integer until which the slicing takes place. The slicing stops at index stop - 1.
  • step - integer value which determines the increment between each index for slicing

If a single parameter is passed, start and step are set to None.


Return value from slice()

slice() returns a slice object used to slice a sequence in the given indices.


Example 1: Create a slice object for slicing

# contains indices (0, 1, 2)
print(slice(3))

# contains indices (1, 3)
print(slice(1, 5, 2))

When you run the program, the output will be:

slice(None, 3, None)
slice(1, 5, 2)

Example 2: Get substring from a given string using slice object

pyString = 'Python'

# contains indices (0, 1, 2)
# i.e. P, y and t
sObject = slice(3)

print(pyString[sObject])

# contains indices (1, 3)
# i.e. y and h
sObject = slice(1, 5, 2)

print(pyString[sObject])

When you run the program, the output will be:

Pyt
yh

Example 3: Get substring from a given string using negative index

pyString = 'Python'

# contains indices (-1, -2, -3)
# i.e. n, o and h
sObject = slice(-1, -4, -1)

print(pyString[sObject])

When you run the program, the output will be:

noh

Example 4: Get sublist and sub-tuple from a given list and tuple respectively

pyList = ['P', 'y', 't', 'h', 'o', 'n']
pyTuple = ('P', 'y', 't', 'h', 'o', 'n')

# contains indices (0, 1, 2)
# i.e. P, y and t
sObject = slice(3)

# slice a list
print(pyList[sObject])

# contains indices (1, 3)
# i.e. y and h
sObject = slice(1, 5, 2)

# slice a tuple
print(pyTuple[sObject])

When you run the program, the output will be:

['P', 'y', 't']
('y', 'h')

Example 5: Get sublist and sub-tuple from a given list and tuple respectively using negative index

pyList = ['P', 'y', 't', 'h', 'o', 'n']
pyTuple = ('P', 'y', 't', 'h', 'o', 'n')

# contains indices (-1, -2, -3)
# i.e. n, o and h
sObject = slice(-1, -4, -1)

# slice a list
print(pyList[sObject])

# contains indices (-1, -3)
# i.e. n and h
sObject = slice(-1, -5, -2)

# slice a tuple
print(pyTuple[sObject])

When you run the program, the output will be:

['n', 'o', 'h']
('n', 'h')

Example 6: Get substring from a given string by extending indexing syntax

The slice object can be substituted with the indexing syntax in Python.

You can alternately use the following syntax for slicing:

obj[start:stop:step]
pyString = 'Python'

# contains indices (0, 1, 2)
# i.e. P, y and t
print(pyString[0:3])

# contains indices (1, 3)
# i.e. y and h
print(pyString[1:5:2])

When you run the program, the output will be:

Pyt
yh