The syntax is:

Use with AIa[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

Use with AIa[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

Use with AIa[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

Use with AIa[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

Use with AIa[start:stop:step]

is equivalent to:

Use with AIa[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

Answer from Greg Hewgill on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_func_slice.asp
Python slice() Function
❮ Previous Python Function Reference Next ❯ · Create a tuple and a slice object. Use the slice object to get only the two first items of the tuple: a = ("a", "b", "c", "d", "e", "f", "g", "h") x = slice(2) print(a[x]) Try it Yourself » · Share Link Copied ·
🌐
Python Morsels
pythonmorsels.com › slicing
Python list slicing (with examples) - Python Morsels
August 3, 2026 - In Python, slicing uses colons in square brackets to grab part of a list, string, or tuple: first items, last items, reverse, and step.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-slice-function
Python slice() function - GeeksforGeeks
June 8, 2026 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 8 Jun, 2026 · slice() function is used to create a slice object that specifies how to extract a portion of a sequence such as a string, list or tuple.
🌐
Programiz
programiz.com › python-programming › methods › built-in › slice
Python slice() | Programiz
March 14, 2024 - The slice() function returns a slice object that is used to slice any sequence (string, tuple, list, range, or bytes).
Top answer
1 of 16
6670

The syntax is:

Use with AIa[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

Use with AIa[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

Use with AIa[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

Use with AIa[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

Use with AIa[start:stop:step]

is equivalent to:

Use with AIa[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

2 of 16
729

The Python tutorial talks about it (scroll down a bit until you get to the part about slicing).

The ASCII art diagram is helpful too for remembering how slices work:

Use with AI +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
   0   1   2   3   4   5
  -6  -5  -4  -3  -2  -1

One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n.

🌐
DataCamp
datacamp.com › home › tutorials › python › python slice: useful methods for everyday coding
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - As I said, slicing is a core feature in Python, allowing developers to extract portions of sequences like lists, strings, and tuples. Python offers two primary ways to slice sequences without importing anything: the slicing : syntax and the slice() function.
🌐
Codecademy
codecademy.com › docs › python › built-in functions › slice()
Python | Built-in Functions | slice() | Codecademy
September 26, 2025 - The slice() function creates a slice object that specifies how to slice sequences like strings, lists, tuples, and ranges. It provides a way to extract specific portions of sequences by defining the start, stop, and step parameters, offering ...
Find elsewhere
🌐
Python
docs.python.org › 3 › c-api › slice.html
Slice Objects — Python 3.15.2 documentation
The type object for slice objects. This is the same as slice in the Python layer.
🌐
Real Python
realpython.com › ref › glossary › slice
slice | Python Glossary – Real Python
In Python, a slice is an object that allows you to extract a portion of a sequence, such as a list, tuple, or string.
🌐
The Python Coding Stack
thepythoncodingstack.com › notes from the near future › a slicing story
A Slicing Story - The Python Coding Stack
September 24, 2023 - You can read more about __getitem__() in The Manor House, the Oak-Panelled Library, the Vending Machine, and Python's `__getitem__()`. In this new class called TestList, which inherits from list, you first print the value and data type of the argument in the __getitem__() method, and then you call the list's __getitem__() method and return its value. This is what super().__getitem__(item) does since list is the superclass for TestList. The syntax 2:7 within the square brackets represents a slice object.
🌐
Railsware
railsware.com › home › engineering › indexing and slicing for lists, tuples, strings, other sequential types in python
Python Indexing and Slicing for Lists, Tuples, Strings, other Sequential Types | Railsware Blog
September 28, 2026 - Python supports slice notation for any sequential data type like lists, strings, tuples, bytes, bytearrays, and ranges. Also, any new data structure can add its support as well. This is greatly used (and abused) in NumPy and Pandas libraries, which are so popular in Machine Learning and Data Science.
🌐
Simplilearn
simplilearn.com › home › resources › software development › your ultimate python tutorial for beginners › everything you need to know about python slicing
Everything You Need to Know About Python Slicing
January 26, 2025 - Python is a widely used, general-purpose programming language. So what is slicing and negative indices? Learn all about Python slicing, index, & more. Read On!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python slicing in depth
Python Slicing in Depth
March 27, 2025 - Everything in Python is an object including the slice. A slice is actually an object of the slice type.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-slicing
Python List Slicing - GeeksforGeeks
Explanation: slice starts at index 1 and stops before index 4, returning the elements at indices 1, 2, and 3.
Published: July 16, 2026
🌐
Dot Net Perls
dotnetperls.com › slice-python
Python - Slice Examples - Dot Net Perls
A Python slice extracts elements, based on a start and stop.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Slice a List, String, and Tuple in Python | note.nkmk.me
May 6, 2023 - Reverse a list, string, tuple in Python (reverse, reversed) You can create a slice object using the built-in slice() function.
🌐
Python Central
pythoncentral.io › how-to-slice-listsarrays-and-tuples-in-python
How to Slice Lists/Arrays and Tuples in Python | Python Central
July 18, 2022 - Getting the first “N” elements from a list with slice is as simple as using the following syntax: ... Let’s take the same list as the example before, and output the first three elements of the list by slicing: [python] items = ['car', 'bike', 'house', 'bank', 'purse', 'photo', 'box'] sub_items = items[:3] print(sub_items) [/python]
🌐
Sentry
sentry.io › sentry answers › python › slicing in python
Python: How Slice Notation Works | Sentry
September 24, 2026 - Python’s slice notation provides a quick way for programmers to extract sections of sequences, such as lists and strings.