Python slice() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, slice(start, stop, step) creates a slice object that describes a range of positions. seq[slice(1, 6, 2)] is the same as seq[1:6:2]. With one argument, slice(stop) starts at the beginning.

Slice objects let you name and reuse a slice, which makes code that cuts fixed-width text much easier to read. They expose .start, .stop, and .step, and indices(length) converts them into concrete numbers for a sequence of that length.

slice is a built-in type (a class) that you call like a function: each call creates a new slice object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Create a slice, then use it inside square brackets.

python
first_three = slice(3)
every_other = slice(0, None, 2)
part = sequence[every_other]
CallWhat it does
slice(3)Same as [:3].
slice(2, 5)Same as [2:5].
slice(None, None, -1)Same as [::-1] (reverse).
s.indices(10)Returns (start, stop, step) for length 10.

None in a slice means “use the default”, exactly like leaving a number out of [start:stop:step].

Library declarations
python
slice(stop, /)
slice(start, stop, step=None, /)
# class slice: a built-in type

Parameters

ParameterDescription
startOptional. First index (inclusive). None means the beginning.
stopEnd index (exclusive). None means the end.
stepOptional. Distance between items; negative goes backwards. None means 1.

Return Value

Return ValueDescription
sliceA slice object with start, stop, and step attributes.

Python Examples

Example 1 — Store Slices in Variables

Create three slice objects and apply them to a string.

python
letters = "abcdefgh"
first_three = slice(3)
evens = slice(0, None, 2)
tail = slice(-2, None)

print(letters[first_three])
print(letters[evens])
print(letters[tail])
print(first_three)
print(letters[1:6:2] == letters[slice(1, 6, 2)])

How It Works

  1. One argument is stop. slice(3) means [:3], the first three letters.
  2. Step of 2. slice(0, None, 2) takes every other letter from the start.
  3. Negative start. slice(-2, None) keeps the last two letters.
  4. Readable repr. Printing a slice shows all three parts, and it matches the colon syntax exactly.

Example 2 — Parse Fixed-Width Records with Named Slices

Give each column of a fixed-width line a name and reuse it for every record.

python
records = [
    "2024-03-09 Ana   42",
    "2024-03-10 Ben  108",
]
DATE = slice(0, 10)
NAME = slice(11, 16)
SCORE = slice(16, 19)

for line in records:
    print(line[DATE], line[NAME].strip(), int(line[SCORE]))

How It Works

  1. Columns by position. The date is characters 0 to 9, the name 11 to 15, and the score 16 to 18.
  2. Named once. DATE, NAME, and SCORE document the layout better than bare numbers.
  3. Clean the pieces. strip() drops padding from the name and int() ignores the leading space of " 42".
  4. Same slices, every line. The loop applies the stored slices to each record.

Example 3 — Inspect a Slice with indices()

Read a slice's parts and resolve them for a concrete length, including a reversing slice.

python
s = slice(1, None, 2)
print(s.start, s.stop, s.step)
print(s.indices(10))
print(list(range(10))[s])

backwards = slice(None, None, -1)
print("python"[backwards])
print(backwards.indices(6))

How It Works

  1. Raw attributes. start, stop, and step are exactly what was passed, including None.
  2. Resolved for a length. indices(10) replaces None with 10, giving (1, 10, 2).
  3. Odd positions. Applying the slice to 0..9 picks 1, 3, 5, 7, 9.
  4. Reverse slice. A step of -1 reverses the text; for length 6 it starts at 5 and runs past index 0.

Frequently Asked Questions

An object that stores start, stop, and step. seq[slice(1, 4)] does the same as seq[1:4].
To give a slice a name and reuse it, for example when parsing fixed-width columns, or to pass a slice into a function.
It converts the slice into concrete (start, stop, step) values for a given sequence length, handling None and negative numbers.
Use items[slice(None, None, -1)], which equals items[::-1]. reversed(items) is an alternative that does not copy.
Yes, safely. "abc"[slice(0, 100)] simply returns "abc" without raising IndexError.

Next: sorted()

Return a new sorted list from any iterable.

sorted() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful