Python slice()

Python slice() builtin function returns an object of type slice. This slice object can be used to slice a Python sequence such as string, tuple, list, etc.

In this tutorial, we will learn about the syntax of Python slice() function, and learn how to use this function with the help of examples.

Syntax

The syntax of slice() function with stop parameter is

slice(stop)

where

ParameterRequired/OptionalDescription
stopRequiredAn integer. The index up until which the slice object contains the indices.

The syntax of slice() function with start, stop and an optional step parameter is

slice(start, stop[, step])

where

ParameterRequired/OptionalDescription
valueRequiredAn integer. The staring index from which the slice object contains the indices.
stopRequiredAn integer. The index up until which the slice object contains the indices.
stepOptionalAn integer. The step value is the difference between any two adjacent indices. The default step in a slice object is 1.

Returns

The function returns an object of type slice.

ADVERTISEMENT

Examples

1. slice(stop)

In this example, we call slice() function and give a value, say 4, for stop parameter. The function returns a slice object. Let us print that slice object.

Python Program

stop = 3
slice_object = slice(stop)
print(slice_object)
Try Online

Output

slice(None, 3, None)

Now, let us use this slice object to slice a sequence, say a string.

Python Program

stop = 3
slice_object = slice(stop)

x = 'abcdefgh'
print(x[slice_object])
Try Online

Output

abc

Explanation

'a  b  c  d  e  f  g  h'  :input string
 0  1  2  3  4  5  6  7   :indices
          |stop=3
 0  1  2                  :indices in slice object
 a  b  c                  :resulting string of x[slice_object]

2. slice(start, stop)

In this example, we call slice() function and get a slice object with start=2 and stop=5. We will use this slice object, to slice a string.

Python Program

start = 2
stop = 5
slice_object = slice(start, stop)

x = 'abcdefgh'
print(x[slice_object])
Try Online

Output

cde

Explanation

'a  b  c  d  e  f  g  h'  :input string
 0  1  2  3  4  5  6  7   :indices
start=2|        |stop=5
       2  3  4            :indices in slice object
       c  d  e            :resulting string of x[slice_object]

3. slice(start, stop, step)

In this example, we call slice() function and get a slice object with start=2 and stop=9 and step=2. We will use this slice object, to slice a string.

Python Program

start = 2
stop = 9
step = 2
slice_object = slice(start, stop, step)

x = 'abcdefghijklm'
print(x[slice_object])
Try Online

Output

cegi

Explanation

'a  b  c  d  e  f  g  h  i  j  k  l  m'  :input string
 0  1  2  3  4  5  6  7  8  9 10 11 12   :indices
start=2|                    |stop=9
       2     4     6     8               :indices in slice object with step=2
       c     e     g     i               :resulting string of x[slice_object]

Conclusion

In this Python Tutorial, we have learnt the syntax of Python slice() builtin function, and also learned how to use this function, with the help of Python example programs.