Negative slicing Python

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.
Screenshot_32

fav_fruit = "mango"
print(fav_fruit[-3:-1])

here it prints as:

ng

why the letter “o” is not printed?

Hello @thiyamsureshsingh, the reason only ng is shown is this; in Python, list (and therefore string) slicing is exclusive. That means that the slice returns the elements from the start to the element just before the stop:

list_to_be_sliced = ["a", "b", "c", "d", "e", "f"]
print(list_to_be_sliced[1:4])#Will return elements at index 1, 2 and 3 (just 
#before 4). Therefore it will print ["b", "c", "d"], but not the element at
#index 4, "e"

The same goes for negative indexing.

list_to_be_sliced = ["a", "b", "c", "d", "e", "f"]
print(list_to_be_sliced[-4:-2])#This goes from the fourth from last element 
#up to, but not including, the second last element.
#It will print: ["c", "d"]

I hope this helps!

Remember that when you apply slicing the second value is not included and python takes the previous value even if it’s negative. In your example i think you should have used fav_fruit([-3:]) that would print ngo

so , it means in negative slicing, it does not check in index number? but it checks the element position?

here for positive slicing,
print(list_to_be_sliced[1:4])
here “1” is considered as index number , for which the value is “b”

but see here in negative:

print(list_to_be_sliced[-4:-2])

“-4” this value is not “b” , the value is “c”, so does it mean in negative , it doesn’t check in index value?

Hello @thiyamsureshsingh, sorry for my mis-communication. No, the list slicing still looks at indexes, but they start from the end:

list1 = ["A", "B", "C", "D", "E"]
#The negative indexes go:
#"E" is -1.
#"D" is -2.
#etc.

The way it works is the same as normal indexing, except it begins with -1 as the index for the last object in the list, and continues that way.
I hope this helps!

in negative , the indexing starts at “-1”, right?

reply sir my last post

Yes, because index 0 is the first element of the list when you’re referencing it the “normal” (i.e. positive indexing) way.

@codeneutrino has already answered your question, though.

Here are the relevant docs for slicing lists (and other sequence types) in Python. Please pay attention specifically to Note #4 which reads:

  1. The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j . If i or j is greater than len(s) , use len(s) . If i is omitted or None , use 0 . If j is omitted or None , use len(s) . If i is greater than or equal to j , the slice is empty.

In your slice, fav_fruit[-3:-1], as explained in the docs the element at index -1 will not be included in the slice. (This is what codeneutrino has already told you.)

Your string can be characterised like this:

m a n g o
-5 -4 -3 -2 -1

The string is on the top row, and each letter’s negative index below.

If we say that our slice is of the form [i:j], as explained in the docs our slice will return all elements where k == i <= k < j. So, when you do fav_fruit[-3:-1], Python checks each character in your string against this constraint:

i = -3
j = -1

k = -5:  -3 <= -5 < -1 : false
k = -4:  -3 <= -4 < -1 : false
k = -3:  -3 <= -3 < -1 : TRUE - add "n" to slice
k = -2:  -3 <= -2 < -1 : TRUE - add "g" to slice
k = -1:  -3 <= -1 < -1 : false

So, python correctly returns ng.

Thanks for the explanation, I got it now.
Love from Manipur, India.
Yaiphare!!! :slight_smile: