Yes, calling s[0:-1] is exactly the same as calling s[:-1].

Using a negative number as an index in python returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).

so if you have a list as so:

myList = ['a', 'b', 'c', 'd', 'e']
print myList[-1] # prints 'e'

the print statement will print "e".

Once you understand that (which you may already, it's not entirely clear if that's one of the things you're confused about or not) we can start talking about slicing.

I'm going to assume you understand the basics of a slice along the lines of myList[2:4] (which will return ['c', 'd']) and jump straight into the slicing notation where one side is left blank.

As you suspected in your post, myList[:index] is exactly the same as myList[0:index].

This is also works the other way around, by the way... myList[index:] is the same as myList[index:len(myList)] and will return a list of all the elements from the list starting at index and going till the end (e.g. print myList[2:] will print ['c', 'd', 'e']).

As a third note, you can even do print myList[:] where no index is indicated, which will basically return a copy of the entire list (equivalent to myList[0:len(myList)], returns ['a', 'b', 'c', 'd', 'e']). This might be useful if you think myList is going to change at some point but you want to keep a copy of it in its current state.

If you're not already doing it I find just messing around in a Python interpreter a whole bunch a big help towards understanding these things. I recommend IPython.

Answer from Redwood on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ slicing-with-negative-numbers-in-python
Slicing with Negative Numbers in Python - GeeksforGeeks
September 18, 2025 - stop: Ending index (exclusive). step: Step value (negative step reverses the sequence). Example: Here we use the slice() function to achieve the same slicing as before. Python ยท text = "GeeksforGeeks" # Slicing left part of string text left = text[slice(-8)] # Slicing middle part of string text middle = text[slice(-8, -5)] # Slicing right part of string text right = text[slice(-5, None)] print(left) print(middle) print(right) Output ยท
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_string_negative_indexing.asp
Python String Negative Indexing
Remove List Duplicates Reverse ... Python Bootcamp Python Training ยท โฎ Python Glossary ยท Use negative indexes to start the slice from the end of the string: Get the characters from position 5 to position 1, starting the ...
Top answer
1 of 9
47

Yes, calling s[0:-1] is exactly the same as calling s[:-1].

Using a negative number as an index in python returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).

so if you have a list as so:

myList = ['a', 'b', 'c', 'd', 'e']
print myList[-1] # prints 'e'

the print statement will print "e".

Once you understand that (which you may already, it's not entirely clear if that's one of the things you're confused about or not) we can start talking about slicing.

I'm going to assume you understand the basics of a slice along the lines of myList[2:4] (which will return ['c', 'd']) and jump straight into the slicing notation where one side is left blank.

As you suspected in your post, myList[:index] is exactly the same as myList[0:index].

This is also works the other way around, by the way... myList[index:] is the same as myList[index:len(myList)] and will return a list of all the elements from the list starting at index and going till the end (e.g. print myList[2:] will print ['c', 'd', 'e']).

As a third note, you can even do print myList[:] where no index is indicated, which will basically return a copy of the entire list (equivalent to myList[0:len(myList)], returns ['a', 'b', 'c', 'd', 'e']). This might be useful if you think myList is going to change at some point but you want to keep a copy of it in its current state.

If you're not already doing it I find just messing around in a Python interpreter a whole bunch a big help towards understanding these things. I recommend IPython.

2 of 9
19
>>> l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz&']

# I want a string up to 'def' from 'vwx', all in between
# from 'vwx' so -2;to 'def' just before 'abc' so -9; backwards all so -1.
>>> l[-2:-9:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

# For the same 'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
>>> l[7:0:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

Please do not become listless about list.

  1. Write the first element first. You can use positive or negative index for that. I am lazy so I use positive, one stroke less (below 7, or -3 for the start).
  2. Index of the element just before where you want to stop. Again, you can use positive or negative index for that (below 2 or -8 for stop).
  3. Here sign matters; of course - for backwards; value of stride you know. Stride is a 'vector' with both magnitude and direction (below -1, backwards all).

    l = [0,1,2,3,4,5,6,7,8,9]
    l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1]
    

    All result in [7, 6, 5, 4, 3].

๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ python tutorial โ€บ slicing with negative numbers in python
Slicing with Negative Numbers in Python | PrepInsta
September 9, 2023 - Letโ€™s have a look at the python codes for slicing on the tuple. ... #Initialize the String String = tuple(['P', 'r', 'e', 'p', 'I', 'n', 's', 't', 'a']) #Slicing using 1 negative index arr = String[-2:] print(arr) #Slicing using 1 negative index arr = String[:-3] print(arr) #Slicing using 3rd negative index arr = String[::-1] print(arr) #Slicing using 2 negative indexes arr = String[-9:-4] print(arr)
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
September 29, 2025 - By including only the index number before the colon and leaving the second index number out of the syntax, the substring will go from the character of the index number called to the end of the string. You can also use negative index numbers to slice a string.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Negative Slicing in Python - Python Help - Discussions on Python.org
September 8, 2023 - In this code, -1 index is 3, and -2 index is 2, right? But since we donโ€™t include the second index itself, then we only have one value which is 3. However, why is the result an empty list? nums = [1,2,3] vals = nums [-1โ€ฆ
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ python slice notation
Python slice notation | Sentry
October 21, 2022 - In the example below, a string is sliced starting at index 2, stopping when the array is complete, and taking the values at every second step. num_string = "0123456" even_nums = num_string[2::2] print(even_nums) ... Slicing can use negative indexing.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-a-negative-indexing-in-python
What is a Negative Indexing in Python?
August 25, 2023 - Negative indexing in Python allows you to access elements from the end of a sequence (string, list, tuple) by using negative numbers. Instead of counting from the beginning (0, 1, 2...), negative indexing counts backwards from the last element (-1,
Find elsewhere
๐ŸŒ
Manifoldapp
cuny.manifoldapp.org โ€บ read โ€บ how-to-code-in-python-3 โ€บ section โ€บ 93337ae8-329e-493b-b15a-7fe26f12f037
How To Index and Slice Strings | How To Code in Python 3 | Manifold @CUNY
By including only the index number before the colon and leaving the second index number out of the syntax, the substring will go from the character of the index number called to the end of the string. You can also use negative index numbers to slice a string.
๐ŸŒ
YouTube
youtube.com โ€บ watch
String Slicing using a Negative Index || Start : Stop : Step Index || What? How? || Python Tutorial - YouTube
This video will discuss WHAT and HOW of String Slicing using a Negative Index with fun animation and examples.Please subscribe to our channel for more such v...
Published ย  December 24, 2021
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ how-to-slice-and-index-strings-in-python
How to Slice and Index Strings in Python | Linode Docs
January 28, 2022 - Python uses a string index to retrieve ... the index from zero and from left to right. Negative indexing decrements the index from -1 running from right to left....
๐ŸŒ
Codecademy Forums
discuss.codecademy.com โ€บ get help โ€บ python
Negative slicing Python - Python - Codecademy Forums
May 10, 2020 - Why in negative slicing in my screenshot the last index value is not shown? Someone explain me. I am having some confusion on last index one. fav_fruit = "mango" print(fav_fruit[-3:-1]) here it prints as: ng why the letter โ€œoโ€ is not printed?
๐ŸŒ
Shiksha
shiksha.com โ€บ home โ€บ it & software โ€บ it & software articles โ€บ programming articles โ€บ slicing in python
Slicing in Python - Shiksha Online
November 25, 2022 - Let us retrieve the last 6 characters from a string. To do so you need to give the negative left offset. As you already know you can use negative index values to perform slicing operations.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Negative indexing python code - Python Help - Discussions on Python.org
June 11, 2025 - If anyone can please explain how the code in red square exactly works.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings_slicing.asp
Python - Slicing Strings
Use negative indexes to start the slice from the end of the string: ... 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 ...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to slice strings in python?
How to Slice Strings in Python? - AskPython
August 6, 2022 - Negative indexing starts from -1 to -n(n is the length for the given string). Note, negative indexing is done from the other end of the string. Hence, to access the first character this time we need to follow the below-given code.
Top answer
1 of 5
7

0 is the start of the sequence. Always, unambiguously. Changing its meaning to sometimes be the end would lead to a lot of confusion, especially when using variables for those two values.

Using negative indices is also not a different mode; negative indices are converted to positive indices relative to the length. Changing what element 0 refers to because the other slice input (start or stop) was a negative number makes no sense.

Because 0 always means the first element of the sequence, and there is no spelling for a negative zero, you cannot use 0 to mean the end of the sequence.

You can use None as the stop element to mean this instead, if you need to parameterise your indices:

start = -3
stop = None
result = a[start:stop]

You can also create a slice() object; the same rules apply for how indices are interpreted:

indices = slice(-3, None)
result = a[indices]

In fact, the interpreter translates the slice notation into a slice() object, which is then passed to the object to distinguish from straight-up indexing with a single integer; the a[start:stop] notation translates to type(a).__getitem__(a, slice(start, stop)) whereas a[42] becomes type(a).__getitem__(a, 42).

So by using a slice() object you can record either slicing or single-element indexing with a single variable.

2 of 5
1

It is boring to use negative slice in a loop if there is some chance to slice to 'negative zero', because [:-0] is not interpreted as expected.

But there is a simple way to solve the problem, just convert negative index to positive index by adding the length of the container.

E.g. Negative Slice Loop:

a = np.arange(10)
for i in range(5):
    print(a[5-i:-i])

Answer:

[]
[4 5 6 7 8]
[3 4 5 6 7]
[2 3 4 5 6]
[1 2 3 4 5]

Convert to positive by adding the lenght:

for i in range(5):
    print(a[5-i:len(a)-i])

Get the right answer:

[5 6 7 8 9]
[4 5 6 7 8]
[3 4 5 6 7]
[2 3 4 5 6]
[1 2 3 4 5]
๐ŸŒ
Cse163
cse163.github.io โ€บ book โ€บ module-1-introduction-to-python โ€บ lesson-3-strings-and-lists โ€บ negative-indices.html
Negative Indices โ€” Intermediate Data Programming
Asking Python to go โ€ n before ... uses negative numbers! The idea is you start at the last character ( 'd' ) being at index -1 (since it is at index len(s) - 1 in our indexing scheme), the second to last ( 'l' ) being -2 , etc....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-slicing-in-python
String Slicing in Python - GeeksforGeeks
A positive value slices from left to right, while a negative value slices from right to left. If omitted, it defaults to 1 (no skipping of characters). Return Type: The result of a slicing operation is always a string (str type) that contains ...
Published ย  July 12, 2025