0

I need to write python code which uses slicing to return "ze" from the string "Malwarez!" I wrote this:

spam = "Malwarez!"
X = spam[-2:5:-1]
print(X)

However, I just came to the end result by randomly guessing the steps and stops. Can someone please explain how reverse negative indexing work?

Thx!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    It starts from -2, which is second from the end (which is the "z"), position 7 (from 0), and it counts backwards until (but not including) position 5. That gives you 6 and 7 in reverse order. You could also use [-2:-4:-1], or [7:5:-1]. – Tim Roberts Oct 01 '22 at 04:18

0 Answers0