The reason why f[0:5:-1]
does not generate any output is because you are starting at 0, and trying to count backwards to 5. This is impossible, so Python returns an empty string.
Instead, you want f[5:0:-1]
, which returns the string "raboo"
.
Notice that the string does not contain the f
character. To do that, you'd want f[5::-1]
, which returns the string "raboof"
.
You also asked:
I have read start should not pass stop. is that true in case of negative step value also?
No, it's not true. Normally, the start
value shouldn't pass the stop
value, but only if the step is positive. If the step is negative, then the reverse is true. The start
must, by necessity, be higher then the stop
value.