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
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 - In the below python code we will be iterating through negative indexes of the string elements. ... #Initialize the String String = 'PrepInsta' #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)
Discussions

python - When using negative numbers to slice a string, why is 0 is disabled? - Stack Overflow
One way to remember how slices ... has index n, for example: +---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 · The first row of numbers gives the position of the indices 0...6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively. So when we use negative indices in loop we should ... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
String Slicing with Negatives in Python - Stack Overflow
lets say I have this variable: a = "amazingjob" and I want to slice the last three letters, but I want to do it backwards/using negative numbers, meaning: a[-1:-4] I want the result to be job More on stackoverflow.com
🌐 stackoverflow.com
Negative Slicing in Python
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:-2] print (vals) Thank you in advance. More on discuss.python.org
🌐 discuss.python.org
3
0
September 8, 2023
Negative indexing python code
If anyone can please explain how the code in red square exactly works. More on discuss.python.org
🌐 discuss.python.org
5
0
June 11, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › slicing-with-negative-numbers-in-python
Slicing with Negative Numbers in Python | GeeksforGeeks
December 2, 2024 - We can perform slicing in Python using the colon ':' operator. It accepts three parameters which are start, end, and step. Start and end can be any valid index whether it is negative or positive.
🌐
W3Schools
w3schools.com › python › gloss_python_string_negative_indexing.asp
Python String Negative Indexing
Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans ... Python Operators Arithmetic Operators Assignment Operators Ternary Operator Comparison Operators Logical Operators Identity Operators Membership Operators Bitwise Operators Operator Precedence Code Challenge Python Lists · Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises Code Challenge Python Tuples
🌐
CodeChef
codechef.com › learn › course › python-development › PYDEV09 › problems › PYTHPROB361C
Negative Slicing in Python for project building
In Python, you can slice strings using negative indices to count from the end instead of the beginning.
🌐
EyeHunts
tutorial.eyehunts.com › home › negative slicing in python | example code
Negative slicing in Python | Example code - EyeHunts
October 21, 2021 - Using a negative number as an index in the slice method is called Negative slicing in Python. It returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).
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]
Find elsewhere
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › slicing in python
Slicing in Python - Shiksha Online
November 25, 2022 - However, we only used a positive step (or stride). You can also use a negative stride. By using negative steps, the first two bounds are essentially reversed. Reversing is one of the most common use cases from three-limit slices.
🌐
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…
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
Learn how to index and slice strings in Python 3 with step-by-step examples. Master substring extraction, negative indexing, and slice notation.
🌐
AlgoCademy
algocademy.com › link
String Negative Index in Python | AlgoCademy
When working with negative indexing, it's important to test your code thoroughly to ensure it handles edge cases correctly. Here are some tips: Use print statements to verify the values of indices and slices. Write test cases to check the behavior of your code with different input sizes and edge cases. import unittest class TestNegativeIndexing(unittest.TestCase): def test_string_indexing(self): self.assertEqual("ficus"[-1], 's') self.assertEqual("ficus"[-3], 'c') def test_list_indexing(self): fruits = ["apple", "banana", "cherry", "date"] self.assertEqual(fruits[-1], "date") self.assertEqual(fruits[-2:], ["cherry", "date"]) def test_slicing(self): message = "Hello world" self.assertEqual(message[-4:], "orld") self.assertEqual(message[-7:], "o world") if __name__ == "__main__": unittest.main()
🌐
Hostman
hostman.com › tutorials › slicing-and-indexing-strings-in-python
Slicing and Indexing Strings in Python
The syntax for slicing is: ... Negative indexing in Python allows you to count from the end of the string. This is particularly useful when you want to access characters from the back of the string without knowing its exact length.
🌐
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,
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-slicing-in-python
String Slicing in Python - GeeksforGeeks
Negative indexing makes it easy to get items without needing to know the exact length of the string. ... s[-4:] slices the string starting from the 4th character from the end ('m') to the end of the string.
Published: July 12, 2025
🌐
Reddit
reddit.com › r/learnpython › working with negative indexes and a while statement
r/learnpython on Reddit: Working with negative indexes and a while statement
January 24, 2025 -

HI all,

I am attempting to make a loop which starts at the end of the string [-1] and prints off the characters from the end of the string till the beginning of the string. The issue I am having is with the while statement, negative indexing and an infinite loop.

As you can see the way the loop is set up now it will never reach the len(string) since it's counting backwards and each letter is string[-number]. So how does one work with a loop when indexing backwards? I know these are stupid questions but I'm trying.

string = input("Please type in a string:")
index = 0
while index < len(string):
    index -= 1
    print (string[index])
🌐
Knowledgehills
knowledgehills.com › python › negative-indexing-slicing-stepping-comparing-lists.htm
Python Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and Min – Knowledge Hills
Well, the answer is “Alex”, this is because when you give a negative index number Python counts the element from the right. The rightmost element is at the index of -1. Few other programming languages have this negative index, but this feature is extremely useful.
🌐
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.
🌐
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 string indexing can ... To use Python to slice a string from a parent string, indicate the range of the slice using a start index and an end index....