educative.io

Reverse Slicing

There is an example of reverse slicing that I saw in the course:


At line 3 it said

print(my_string[17:0:-2])

I thought the outcome would be

git Ms it

because:

  • the substring has 18 characters
  • so when we type “[17:0:-2]” it means the substring ends before “!”

but the result was actually

!nrsY ish

can sb explain why? sorry for my bad english

Yes, the length of the string is 18 but that means the index range is 0-17. You can execute the statement print(my_string[17]) and it will print !.

Could you please explain the calculation of Line 2