How answer is coming 86

my_string = "0123456789"
print (my_string[-2: -6: -2])

Let’s analyze the 3 values in the [] brackets. Understanding is the key to the result. The first -2 is equivalent to len(my_string) - 2 which is equal to 8. Hence, we have our starting index as 8.

-6 indicates the ending index and is equivalent to len(my_string) - 6) which gives us 4. So our range is from index 8 (start) to 4 (end) which is the substring “5678”.

The last -2 indicates the step. So, initially, we get ‘8’. Then we jump back two places (-2) which gives up ‘6’. Jumping back two places further goes outside the range of the substring, hence, in the end we are left with 86

1 Like