The syntax is:

a[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:

a[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:

a[-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:

a[::-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.:

a[start:stop:step]

is equivalent to:

a[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 โ€บ python_strings_slicing.asp
Python - Slicing Strings
Python Examples Python Compiler ... Python Interview Q&A Python Bootcamp Python Training ... You can return a range of characters by using the slice syntax....
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ brackets โ€บ slicing.html
[] (slicing) โ€” Python Reference (The Right Way) 0.1 documentation
>>> +---+---+---+---+ >>> |-4 |-3 |-2 |-1 | <= negative indexes >>> +---+---+---+---+ >>> | A | B | C | D | <= sequence elements >>> +---+---+---+---+ >>> | 0 | 1 | 2 | 3 | <= positive indexes >>> +---+---+---+---+ >>> |<- 0:3:1 ->| <= extent of the slice: "ABCD"[0:3:1]
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ the python coding stack โ€บ a slicing story
A Slicing Story - by Stephen Gruppetta
June 25, 2024 - 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-slicing-in-python
String Slicing in Python - GeeksforGeeks
String slicing in Python is a way to get specific parts of a string by using start, end and step values. Itโ€™s especially useful for text manipulation and data parsing. ... Explanation: In this example, we used the slice s[0:5] to obtain the ...
Published ย  July 12, 2025
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ slicing
List slicing in Python - Python Morsels
March 8, 2024 - In Python, slicing looks like indexing with colons (:). You can slice a list (or any sequence) to get the first few items, the last few items, or all items in reverse.
๐ŸŒ
University of Pittsburgh
sites.pitt.edu โ€บ ~naraehan โ€บ python3 โ€บ mbb8.html
Python 3 Notes: List Slicing
Python 3 Notes [ HOME | LING 1330/2330 ] Tutorial 8: List Slicing << Previous Tutorial Next Tutorial >> On this page: slice indexing with [:], negative indexing, slice and negative indexing on strings. Video Tutorial Python 3 Changes print(x,y) instead of print x, y Python 2 vs.
Find elsewhere
Top answer
1 of 16
6664

The syntax is:

a[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:

a[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:

a[-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:

a[::-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.:

a[start:stop:step]

is equivalent to:

a[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
728

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:

 +---+---+---+---+---+---+
 | 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.

๐ŸŒ
DEV Community
dev.to โ€บ thrivensunshine โ€บ slicing-in-python-2hlk
Slicing in Python - DEV Community
April 3, 2020 - One of these similarities include the idea of "slicing" some form of data. Slicing does exactly what it implies, it "slices" up data into smaller parts. Every language has their own particular way of doing this. Lets take a look at how Python in particular handles this kind of operation.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ python slice notation
Python slice notation | Sentry
October 21, 2022 - Slicing segments an array and returns that segment. In the example below, a string is segmented to return a single word from within the string: hello_world_string = "Hello World!" world = hello_world_string[7:12] print(world) ...
๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ python โ€บ slicing-in-python
Slicing in Python
July 22, 2024 - In Python a slice object enables users to extract a section of a sequence like a list, string or tuple. By specifying a range of indices users can get a subset that includes elements starting from one point up to but not including, another point.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ slicing-and-indexing-in-python
Slicing and Indexing in Python โ€“ Explained with Examples
December 11, 2025 - In Python, you perform slicing using the colon : operator. The syntax for slicing is as follows: ... where start_index is the index of the first element in the sub-sequence and end_index is the index of the last element in the sub-sequence ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-slicing-how-to-slice-an-array
Python Slicing โ€“ How to Slice an Array and What Does [::-1] Mean?
December 8, 2022 - In our example, the start is 1, the end is 4 and the step is 2. Slicing starts from the value at index 1 which is 2, then the next value will be at the previous index plus the step, which is 1 + 2 equals 3. The value at index 3 is 4 so that is added to the slice.
๐ŸŒ
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
January 22, 2025 - 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.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Slicing in Python is a method for extracting specific portions of sequences like strings, lists, tuples, and ranges. Slicing can refer to using the built-in slice() function or the even more common bracket notation that looks like this: [start:end:step]. The functionality is a core part of ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ c-api โ€บ slice.html
Slice Objects โ€” Python 3.14.6 documentation
Retrieve the start, stop and step indices from the slice object slice, assuming a sequence of length length.
๐ŸŒ
GitConnected
levelup.gitconnected.com โ€บ python-notes-slicing-part-2-41f2e95ebef5
How to slice in Python, Slicing in Python | Level Up Coding
November 26, 2024 - In the first part we learned that slicing in Python can be used to: ... We learned that the slicing syntax is sequence[start:stop:step] and there are some handy shortcuts. In addition to the above, Iโ€™d add 3 more points to what we can use the slicing syntax in for: ... # Assign my_list = [0, 1, 2, 3, 4, 5] my_list[1:4] = [10, 20] # Replace slice [1, 2, 3] with new values [10, 20] print(my_list) # [0, 10, 20, 4, 5]
๐ŸŒ
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
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ builtin-functions โ€บ slice
slice() | Pythonโ€™s Built-in Functions โ€“ Real Python
The built-in slice() function creates a slice object representing a set of indices specified by range(start, stop, step).
๐ŸŒ
DEV Community
dev.to โ€บ marvintensuan โ€บ quick-peek-under-the-hood-indexing-and-slicing-in-python-3a5i
Quick Peek Under the Hood: Indexing and Slicing in Python - DEV Community
March 13, 2022 - from dis import dis dis('[][0]') # Result: # 1 0 BUILD_LIST 0 # 2 LOAD_CONST 0 (0) # 4 BINARY_SUBSCR # 6 RETURN_VALUE dis('[][::-1]') # Result: # 1 0 BUILD_LIST 0 # 2 LOAD_CONST 0 (None) # 4 LOAD_CONST 0 (None) # 6 LOAD_CONST 1 (-1) # 8 BUILD_SLICE 3 # 10 BINARY_SUBSCR # 12 RETURN_VALUE ยท What we're seeing in these snippets is that Subscript is referred to as BINARY_SUBSCR at bytecode level. We can also see that if any of start, stop, or step is not supplied, None is put in place. Part of the Python's builtins is the slice function which generates the