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 - Example: Here we slice a string into left, middle and right parts using negative indices. Python · text = "GeeksforGeeks" # Slicing left part of string text left = text[:-8] # Slicing middle part of string text middle = text[-8:-5] # Slicing right part of string text right = text[-5:] print(left) print(middle) print(right) Output · Geeks for Geeks ·
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].

🌐
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…
🌐
PREP INSTA
prepinsta.com › home › python tutorial › slicing with negative numbers in python
Slicing with Negative Numbers in Python | PrepInsta
September 9, 2023 - #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) Output: ('t', 'a') ('P', 'r', 'e', 'p', 'I', 'n') ('a', 't', 's', 'n', 'I', 'p', 'e', 'r', 'P') ('P', 'r', 'e', 'p', 'I') Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription ... Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
🌐
EyeHunts
tutorial.eyehunts.com › home › negative slicing in python | example code
Negative slicing in Python | Example code - EyeHunts
October 21, 2021 - Python supports using negative numbers to index into a string: -1 means the last char, -2 is the next to last, and so on. Simple example code. ... Calling s[0:-1] is exactly the same as calling s[:-1]. myList = ['A', 'B', 'C', 'D', 'E'] ...
🌐
Reddit
reddit.com › r/learnpython › list slicing with negative index
r/learnpython on Reddit: List slicing with negative index
December 15, 2021 -

Although I know how slicing works but following examples kind of stumped me.

values = [3,4,3,7,8,9,5]
values[:3:-1]    #o/p [5,9,8]
values[5:3:-1]    #o/p [9,8]

I always thought in list[start:stop:step], 'start' defaults to 0, 'stop' defaults to length-1 and 'step' defaults to 1. But this doesn't make sense here. What are the rules? Link to official docs will also be appreciated as I couldn't find that.

🌐
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 …
🌐
TutorialsPoint
tutorialspoint.com › what-is-a-negative-indexing-in-python
What is a Negative Indexing in Python?
String after slicing (negative indexing) = edd · To display the 1st element to last element in steps of 1 in reverse order, we use the [::-1]. The [::-1] reverses the order. ... myStr = 'Hello! How are you?' print("String = ", myStr) # Slice print("Reverse order of the String = ", myStr[::-1])
Find elsewhere
🌐
Medium
medium.com › @tabusheikh278 › mastering-series-indexing-in-python-a-deep-dive-into-integer-negative-slicing-negative-532a7e123fac
“Mastering Series Indexing in Python: A Deep Dive into Integer, Negative, Slicing, Negative Slicing, and Fancy Indexing” | by Thabasum Shaikh | Medium
January 5, 2024 - In this example, the slice includes ... programming, particularly in Python, that involves extracting a sub-sequence from the end of a sequence object using negative indices · The syntax for negative slicing is similar to regular ...
🌐
CodingBat
codingbat.com › doc › python-strings.html
CodingBat Python Strings
The negative numbers work analogously for the chars at the end of the string with -1, -2, etc. working from the right side. s = 'Hello' # -54321 ## negative index numbers s[-2:] ## 'lo', begin slice with 2nd from the end s[:-3] ## 'He', end slice 3rd from the end · One way to loop over a string ...
🌐
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 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
🌐
Sentry
sentry.io › sentry answers › python › python slice notation
Python slice notation | Sentry
Assigning a step of 2 will include ... when the array is complete, and taking the values at every second step. ... Slicing can use negative indexing....
🌐
Bas
bas.codes › posts › python-slicing
A Comprehensive Guide to Slicing in Python - Bas codes
Since we’re using Nones, the slice object needs to calculate the actual index values based on the length of our sequence. Therefore, to get our index triple, we need to pass the length to the indices method, like so: ... This will give us the triple (0, 6, 2). We now can recreate the loop like so: sequence = list("Python") start = 0 stop = 6 step = 2 i = start while i != stop: print(sequence[i]) i = i+step
🌐
LabEx
labex.io › tutorials › python-how-to-slice-lists-with-negative-indices-435398
How to slice lists with negative indices | LabEx
Prefer built-in slicing over manual loops · ## Multi-dimensional data processing matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] ## Extract diagonal elements diagonal = [matrix[i][i] for i in range(len(matrix))] print(diagonal) ## [1, 5, 9] def safe_slice(lst, start=None, end=None): try: return lst[start:end] except IndexError: return "Invalid slice range" At LabEx, we emphasize mastering these slicing techniques for efficient Python programming. By mastering negative indices in Python list slicing, developers can write more concise and readable code.
🌐
AlgoCademy
algocademy.com › link
String Negative Index in Python | AlgoCademy
Negative indexing can be used not only to access individual elements but also to slice sequences.