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

slice - How slicing in Python works - Stack Overflow
How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice? See Why are slice and range upper-bound More on stackoverflow.com
🌐 stackoverflow.com
What is slicing in Python? - Python - Data Science Dojo Discussions
Slicing enables the extraction of a designated range of elements from arrays, lists, strings, or tuples using the syntax [start : stop : step]. Here, start denotes the beginning or starting index, stop indicates the ending index, and step determines the interval between elements in the specified ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
1
0
November 9, 2022
feedback on order slicing in python

If you haven’t already, I’d pose your question to the TWS API Users Group. There are tons of cats using Python that could help. I use the C++ API so sadly, I won’t be of much use.

More on reddit.com
🌐 r/algotrading
7
5
November 30, 2024
I never realized how complicated slice assignments are in Python...
I've never seen code like this. Absolutely wild you can do those kinds of assignments with slices. IMHO the only thing here that really should be supported is overwriting a slice with one of the same size. Curious to hear about other use cases, though. More on reddit.com
🌐 r/Python
32
149
October 4, 2024
🌐
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 - I've always found array slicing in python to be very powerful and useful, and a thing i wish more languages had. For my common use case (dealing with text) it's very fast to code something up. ... I'm very new to Python and one thing I find confusing and unintuitive is that some things, like slices, start counting at 0 but other things like groups in regular expressions start counting at 1.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › brackets › slicing.html
[] (slicing) — Python Reference (The Right Way) 0.1 documentation
Step value of the slice. Defaults to 1. The same as selected. ... >>> +---+---+---+---+ >>> |-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]
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.

🌐
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 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:
Find elsewhere
🌐
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.
🌐
Real Python
realpython.com › ref › glossary › slicing
slicing | Python Glossary – Real Python
In Python, slicing is an operation that allows you to extract portions of sequences like lists, tuples, and strings by specifying a start, stop, and step indices with the syntax sequence[start:stop:step]. In this syntax, start is the index where ...
🌐
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.
🌐
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 ...
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
What is slicing in Python? - Python - Data Science Dojo Discussions
November 9, 2022 - Slicing enables the extraction of a designated range of elements from arrays, lists, strings, or tuples using the syntax [start : stop : step]. Here, start denotes the beginning or starting index, stop indicates the ending index, and step determines ...
🌐
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.
🌐
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 ...
🌐
Data Dependence
datadependence.com › 2016 › 05 › python-sequence-slicing-guide
A Quick Guide to Slicing in Python – Become a Python Ninja
July 6, 2016 - Slicing has incredibly simple syntax; after a sequence type variable inside square brackets you define a start point, an end point and a step, separated by colons like so; var[start:end:step]. However it doesn’t work the way you might first ...
🌐
InterServer
interserver.net › home › python › how to slice strings in python: step-by-step tutorial
How to Slice Strings in Python: Step-by-Step Tutorial - Interserver Tips
April 23, 2026 - Strings are one of the most common data types in Python, and you will use them in almost every program. String slicing is a way to get a part of a string. Instead of using the whole text, you can extract exactly what you need. This makes your code cleaner and more efficient.
🌐
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) ...
🌐
Python Morsels
pythonmorsels.com › implementing-slicing
Implementing slicing - Python Morsels
March 27, 2023 - That way slicing ProxySequence ... ProxySequence(string) >>> p[1:] ProxySequence('bcdefghi') Python's slicing syntax is powered by slice objects....
🌐
W3Schools
w3schools.com › python › exercise.asp
W3Schools Python Exercise
I completed one of the Python exercises on w3schools.com
🌐
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 Bootcamp Python Training ... Create a tuple and a slice object.
🌐
LearnPython.com
learnpython.com › blog › string-slicing-in-python
String Slicing in Python: A Complete Guide | LearnPython.com
In this article, we’ll learn every aspect of slicing a string in Python. Strings are a sequence of characters; slicing allows us to extract a partial sequence from a string. To slice a string in Python, we use character indexes.