๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 509211 โ€บ how-slicing-in-python-works
Newest Questions - Stack Overflow
Consider this example class Foo: def __init__(self): self.attr = 42 obj = Foo() obj.attr # 42 del obj.attr obj.attr # AttributeError I expected del obj.attr to evaluate to del 42 and raise an ...
๐ŸŒ
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 Training ... You can return a range of characters by using the slice syntax.
Find elsewhere
๐ŸŒ
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 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:
๐ŸŒ
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 - The [-1:] notation is equivalent to slice(-1, None) and extracts elements from the last index to the end of the sequence. Since it starts at index -1 (the last element) and goes to the end, it returns a sequence containing only the last element. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
๐ŸŒ
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
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ python-slice
Python Slice Constructor - Python Slice String & Slicing Tuple - DataFlair
April 25, 2026 - We can slice a string in Python using the Python slice() method. We can also specify the interval. Slicing a string may give us a substring when the step size is 1. ... Like in the previous example, we use positive indices here.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-slice-function
Python slice() Function - Learn By Example
April 20, 2020 - # Slice first 5 list items L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] x = slice(5) print(L[x]) # Prints ['a', 'b', 'c', 'd', 'e']
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.