Skip to content

Python slice() Function

How to use Python's slice() function to extract portions of sequences like strings, lists, and tuples with start, stop, and step parameters.

The slice() function returns a slice object that is used to slice any sequence (string, tuple, list, range, or bytes).

Syntax

slice(start, stop, step)

slice() Parameters

Python slice() function parameters:

Parameter Condition Description
start Optional A number to specify start of the slicing. Default is 0.
stop Required A number to specify end of the slicing.
step Optional A number to specify the step. Default is 1.

slice() Return Value

Python slice() function returns a slice object containing the elements in the given range.

Examples

Example 1: Creating a slicing object for slicing

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

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

output

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

Example 2: Slicing with slice(stop)

If you call slice() with just one argument, you can slice a sequence from index 0 up to the specified index. For example:

text = 'Tutorial Reference'

sliced_text = slice(8)   # get slice object: start=0, end=8, step=1
print(text[sliced_text]) # Output: Tutorial

output

Tutorial

Example 3: Specifying Start and Stop Slicing with slice(start, stop)

The slicing starts from 0 by default. However, you can start the slice at another number by adding a start parameter.

text = 'Tutorial Reference'

sliced_text = slice(2, 8) # get slice object: start=2, end=8, step=1
print(text[sliced_text])  # Output: torial

output

torial

Example 4: Specifying Start, Stop, and Step Slicing with slice(start, stop, step)

The step of the slicing is 1 by default. However, you can specify a different step size by adding a step parameter.

text = 'Tutorial Reference'

sliced_text = slice(1, 14, 2) # get slice object: start=1, end=14, step=2
print(text[sliced_text])      # Output: uoilRfr

output

uoilRfr

Example 5: Slice with Negative Indices

You can specify negative indices while slicing.

text = 'Tutorial Reference'

sliced_text = slice(3, -3) # get slice object: start=3, end=-3, step=1
print(text[sliced_text])   # Output: orial Refere

output

orial Refere

Example 6: Reverse a Sequence with slice()

You can reverse a sequence by specifying both start and stop indices as None and a step as -1.

text = 'Tutorial Reference'

sliced_text = slice(None, None, -1) # get slice object: start=None, end=None, step=1
print(text[sliced_text])            # Output: ecnerefeR lairotuT

output

ecnerefeR lairotuT

Example 7: Slicing a String with slice()

As seen in all the previous examples, you can use slice() function with strings:

text = 'Tutorial Reference'

sliced_text1 = slice(8)         # start=0, end=8, step=1
sliced_text2 = slice(2, 8)      # get slice object: start=2, end=8, step=1
sliced_text3 = slice(1, 14, 2)  # get slice object: start=1, end=14, step=2
sliced_text4 = slice(3, -3)     # get slice object: start=3, end=-3, step=1

print(text[sliced_text1]) # Output: Tutorial
print(text[sliced_text2]) # Output: torial
print(text[sliced_text3]) # Output: uoilRfr
print(text[sliced_text4]) # Output: orial Refere

output

Tutorial
torial
uoilRfr
orial Refere

Example 8: Slicing a List with slice()

You can use slice() function with lists:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

sliced_list1 = slice(8)         # start=0, end=8, step=1
sliced_list2 = slice(2, 8)      # get slice object: start=2, end=8, step=1
sliced_list3 = slice(1, 10, 2)  # get slice object: start=1, end=14, step=2
sliced_list4 = slice(3, -3)     # get slice object: start=3, end=-3, step=1

print(my_list[sliced_list1]) # Output: [1, 2, 3, 4, 5, 6, 7, 8]
print(my_list[sliced_list2]) # Output: [3, 4, 5, 6, 7, 8]
print(my_list[sliced_list3]) # Output: [2, 4, 6, 8, 10, 12]
print(my_list[sliced_list4]) # Output: [4, 5, 6, 7, 8, 9]

output

[1, 2, 3, 4, 5, 6, 7, 8]
[3, 4, 5, 6, 7, 8]
[2, 4, 6, 8, 10, 12]
[4, 5, 6, 7, 8, 9]

Example 9: Slicing a Tuple with slice()

You can use slice() function with tuples:

my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

sliced_tuple1 = slice(8)         # start=0, end=8, step=1
sliced_tuple2 = slice(2, 8)      # get slice object: start=2, end=8, step=1
sliced_tuple3 = slice(1, 14, 2)  # get slice object: start=1, end=14, step=2
sliced_tuple4 = slice(3, -3)     # get slice object: start=3, end=-3, step=1

print(my_tuple[sliced_tuple1]) # Output: (1, 2, 3, 4, 5, 6, 7, 8
print(my_tuple[sliced_tuple2]) # Output: (3, 4, 5, 6, 7, 8)
print(my_tuple[sliced_tuple3]) # Output: (2, 4, 6, 8, 10, 12)
print(my_tuple[sliced_tuple4]) # Output: (4, 5, 6, 7, 8, 9)

output

(1, 2, 3, 4, 5, 6, 7, 8)
(3, 4, 5, 6, 7, 8)
(2, 4, 6, 8, 10, 12)
(4, 5, 6, 7, 8, 9)
Navigation