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 › ref_func_slice.asp
Python slice() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... Create a tuple and a slice object.
🌐
Programiz
programiz.com › python-programming › methods › built-in › slice
Python slice()
Now we know about slice objects, let's see how we can get substring, sub-list, sub-tuple, etc. from slice objects. # Program to get a substring from the given string py_string = 'Python' # stop = 3 # contains 0, 1 and 2 indices
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-slice-function
Python slice() function - GeeksforGeeks
June 8, 2026 - Explanation: slice(5) extracts characters from index 0 to 4, while slice(1, 8, 2) selects every second character from index 1 to 7. Example 2: In this example, slice() is used to extract elements from a list.
🌐
freeCodeCamp
freecodecamp.org › news › slicing-and-indexing-in-python
Slicing and Indexing in Python – Explained with Examples
December 11, 2025 - We could also extract all the even numbers from the list using slicing as follows: even_numbers = numbers[1::2] print(even_numbers) # output: [2, 4, 6, 8] In this example, we have used slicing to extract every other element starting from the ...
🌐
DataCamp
datacamp.com › tutorial › python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - # Create a slice object slice_obj = slice(1, 4) # Apply to a list numbers = [10, 20, 30, 40, 50] print(numbers[slice_obj]) # Output: [20, 30, 40] # Apply to a string text = "Python" print(text[slice_obj]) # Output: "yth" Personally, I like using the slice() function because it allows me to ...
Top answer
1 of 16
6667

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

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

🌐
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.
🌐
Real Python
realpython.com › ref › builtin-functions › slice
slice() | Python’s Built-in Functions – Real Python
In this example, slice(0, None, 7) allows you to select every 7th temperature starting from the first element, effectively picking temperatures for the first day of each week. ... In this step-by-step tutorial, you'll learn how to reverse strings ...
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_strings_slicing.asp
Python - Slicing Strings
Use negative indexes to start the slice from the end of the string: ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python slicing in depth
Python Slicing in Depth
March 27, 2025 - For example: ... The slicing seq[start:stop] returns the elements starting at the index start up to the index stop - 1. Therefore, it’s easier to visualize that the indexes are between the elements when you slice the sequence:
🌐
Learn By Example
learnbyexample.org › python-list-slicing
Python List Slicing - Learn By Example
June 20, 2024 - In this example, we start at index 2, which corresponds to the letter ‘c’. Then we slice up to, but not including, index 7. This means we stop just before the letter ‘h’ at index 7. The result is a new list that contains the elements at indices 2 through 6, giving us the desired sequence of letters from ‘c’ to ‘g’. When slicing lists in Python, you can use negative indices as well.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › brackets › slicing.html
[] (slicing) — Python Reference (The Right Way) 0.1 documentation
>>> +---+---+---+---+ >>> |-4 |-3 ... >>> +---+---+---+---+ >>> |<- 0:3:1 ->| <= extent of the slice: "ABCD"[0:3:1] ... It can be read as: get every single one item between indexes 0 and 2 (exclusive). The next example shows usage of the step argument:...
🌐
Codecademy
codecademy.com › docs › python › built-in functions › slice()
Python | Built-in Functions | slice() | Codecademy
September 26, 2025 - Learn the basics of Python 3.13, one of the most powerful, versatile, and in-demand programming languages today. ... The slice() function returns a slice object that can be used to slice any sequence that supports indexing. This example demonstrates how to create a basic slice object and use it to extract elements from a string:
🌐
NetworkLessons
networklessons.com › home › python › python slice function
Python Slice Function
Instead of using [], you can also use slice(). For example: "Gigabit"[0:4] is the same as "Gigabit"[slice(0,4)]. The [] notation looks cleaner to me.
Published: February 9, 2026
🌐
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
docs.python.org › 2.3 › whatsnew › section-slices.html
15 Extended Slices
July 4, 2010 - However, Python's built-in list, tuple, and string sequence types have never supported this feature, raising a TypeError if you tried it. Michael Hudson contributed a patch to fix this shortcoming. For example, you can now easily extract the elements of a list that have even indexes: ... If you have a mutable sequence such as a list or an array you can assign to or delete an extended slice...
🌐
Javatpoint
javatpoint.com › python-slice-function
Python slice() function with Examples - Javatpoint
Python slice() function with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.
🌐
DataFlair
data-flair.training › blogs › python-slice
Python Slice Constructor - Python Slice String & Slicing Tuple - DataFlair
April 25, 2026 - These are all objects that support sequence protocols and implement __getitem__() and __len__(). The slice() function returns a Python Slice Object. Let’s talk about the syntax of Slicing in Python first: ... You’ll see that we have two syntaxes. When we provide only one parameter value, it takes it to be the stop value. This means to start, and the steps are set to None. Let’s take a simple example of Python Slicing.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-slicing
Python List Slicing - GeeksforGeeks
Python · a = [1, 2, 3, 4, 5, 6, 7, 8, 9] b = a[::-1] print(b) Output · [9, 8, 7, 6, 5, 4, 3, 2, 1] Explanation: slice a[::-1] starts from the end of the list and moves backward one element at a time, returning a reversed copy of the list.
Published: July 16, 2026
🌐
Python Morsels
pythonmorsels.com › slicing
Python list slicing (with examples) - Python Morsels
March 8, 2024 - What do you think we might get ... 3? ... >>> fruits[1:3] ['apple', 'lime'] >>> fruits ['watermelon', 'apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange'] With Python's slicing syntax, the first item is the start index, and the second item is the stop index...