0
>>> sample = "asdf"
>>> sample[-1:0]

I thought it would start with the last character up to the first position (but not including). However, it returns '' when I expected f. Why is this happening?

Wright
  • 3,252
  • 9
  • 25
jojojo
  • 3
  • 1

2 Answers2

3

To go backwards towards the first element, you need a third argument to your slice, the stride:

sample[-1:0:-1]

This defaults to 1, which moves forward one step at a time; setting it to -1 moves backwards instead.

Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
0

Remember the way to go about it is start, stop and step in the square brackets, you got the order wrong!

ChameleonX
  • 67
  • 9