>>> 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?
>>> 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?
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.
Remember the way to go about it is start, stop and step in the square brackets, you got the order wrong!