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

🌐
W3Schools
w3schools.com › python › ref_func_slice.asp
Python slice() Function
A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item. ... Create a tuple and a slice object.
Discussions

A Comprehensive Guide to Slicing in Python
The start/end indexing when going in reverse has always taken a level of extra mental effort that I don't like. Like excluding the first and last item using [1:-1] is intuitive to me, but doing the same in reverse by doing [-2:0:-1] annoys me (though I do get why its like that). That is why I tend to do [1:-1][::-1] instead. More on reddit.com
🌐 r/Python
40
356
February 1, 2022
Is string slicing actually useful in real world applications?
If you are doing lot of text processing, then yes. Here's an example from my usecase. I have markdown files with code snippets starting with $ or >>> etc. I have to post process those files to extract only the command that comes after the prompt. Easiest solution is string slicing, since the length of the prompt is fixed. Also, slicing in Python can be applied to lists, tuples, strings, etc. So, even if you are not doing text processing, you can apply the knowledge elsewhere. More on reddit.com
🌐 r/learnpython
8
1
October 13, 2020
People also ask

How does slicing differ from indexing in Python
divstrongSlicing in Pythonstrong extracts a range of elements from a sequence creating a new subset while indexing retrieves a single element from a specific position in the sequence Slicing uses a start and end index whereas indexing uses just onenbspdiv
🌐
scholarhat.com
scholarhat.com › home
What is Slicing in Python?
Can I reverse a list or string using slicing
divstrongYesstrong you can reverse a list or string using slicing by setting the step parameter to strong1strongdiv
🌐
scholarhat.com
scholarhat.com › home
What is Slicing in Python?
Can I use negative indices in slicing
divstrongYesstrong negative indices can be used in slicing to access elements from the end of the sequencenbspdiv
🌐
scholarhat.com
scholarhat.com › home
What is Slicing in Python?
🌐
DataCamp
datacamp.com › tutorial › python-slice
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.
🌐
W3Schools
w3schools.com › python › python_strings_slicing.asp
Python - Slicing Strings
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 Bootcamp Python Training ... You can return a range of characters by ...
🌐
freeCodeCamp
freecodecamp.org › news › slicing-and-indexing-in-python
Slicing and Indexing in Python – Explained with Examples
December 11, 2025 - In this example, we have used slicing to insert a new list [40, 50] at index 4 in the numbers list. The 4:4 slice means that we are inserting the new list at index 4 (that is, before the element at index 4), but not deleting any existing elements.
🌐
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
Find elsewhere
🌐
ScholarHat
scholarhat.com › home
What is Slicing in Python?
September 11, 2025 - Slicing in Python is a method used to extract specific sections of sequences like strings, lists, or tuples. By specifying a range of indices, you may quickly retrieve sub-sections of this sequence.
🌐
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
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › a slicing story
A Slicing Story - by Stephen Gruppetta
June 25, 2024 - The first item in nested_numbers hasn't changed and is the same inner list as before, [0, 1, 2]. However, the first item in subset_of_nested_lists is now the new list [999, 999, 999]. You can read more about shallow and deep copies in an article I wrote in the pre-Substack era: Shallow and Deep Copy in Python and How to Use __copy__(). So, slicing creates a copy of the subset of the sequence, right? Not so fast… · I have used lists in all the examples in this article so far. You can slice other sequences, too:
🌐
DEV Community
dev.to › thrivensunshine › slicing-in-python-2hlk
Slicing in Python - DEV Community
April 3, 2020 - 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.
🌐
Real Python
realpython.com › ref › glossary › slicing
slicing | Python Glossary – Real Python
In this syntax, start is the index where the slice begins, stop is the index where the slice ends (exclusive), and step determines the stride between each element in the slice. If you omit start, stop, or step, then they default to the beginning of the sequence, the end of the sequence, and a step of one, respectively. With slicing, you can quickly obtain a subset of items without modifying the original sequence, making it an essential feature for any Python programmer. Here’s a quick example of how you can use slicing with a list:
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python slicing in depth
Python Slicing in Depth
March 27, 2025 - 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: Everything in Python is an object ...
🌐
LinkedIn
linkedin.com › pulse › what-slicing-python-constantinos-pigiotis
What is slicing in Python?
April 2, 2023 - In #python, slicing is an operation of slicing a list. This means that it can extract some elements from a list using some preferred indexes.
🌐
Analytics Vidhya
analyticsvidhya.com › home › python slicing: 9 useful methods for everyday coding
Python Slicing: 9 Useful Methods for Everyday Coding
May 16, 2025 - Byte sequence comes into the picture when we are using the binary data types, and slicing allows you to extract relevant parts of binary data with ease and efficiency. ... byte_seq = b'Hello, world!'
🌐
C# Corner
c-sharpcorner.com › article › what-is-slicing-in-python-with-example
What is Slicing in Python with Example
May 13, 2024 - # slicing a string s = "Hello, ... # output: (3, 4) If you are In the above example, we create a string, list or tuple and then slice it using the slice operator....
🌐
Medium
medium.com › @timothyjosephcw › what-is-python-slicing-and-how-does-slicing-in-python-work-2788632c5ba0
What is Python Slicing and How Does Slicing in Python Work? | by timothy joseph | Medium
August 22, 2024 - Python slicing, a versatile and creative technique, allows you to extract specific portions of sequences like lists, tuples, and strings. It provides a concise and efficient way to manipulate data, making it a fundamental concept for any Python ...
🌐
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.
🌐
Programiz
programiz.com › python-programming › methods › built-in › slice
Python slice()
Created with over a decade of experience and thousands of feedback. ... The slice() function returns a slice object that is used to slice any sequence (string, tuple, list, range, or bytes). ... start (optional) - Starting integer where the slicing of the object starts.
🌐
Reddit
reddit.com › r/python › a comprehensive guide to slicing in python
r/Python on Reddit: A Comprehensive Guide to Slicing in Python
February 1, 2022 - Though, I will admit that [1:-1][::-1] ... the best example of a time to ignore premature optimization for enhanced readability since it really isn't that superior in readability to [-2:0:-1] even if I personally find it easier to read. ... yeah man. if you're going for absolute optimal performance then you write it in whatever esoteric golang spinoff is trending, but the python way basically values readability over negligible performance benefits. or you could do it the single slice way and leave ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-list-slicing
Python List Slicing - GeeksforGeeks
In Python, list slicing allows out-of-bound indexing without raising errors. If we specify indices beyond the list length then it will simply return the available items. Example: The slice a[7:15] starts at index 7 and attempts to reach index ...
Published   March 8, 2025