3

I am having a problem in understanding what happens when i put negative value to step in case of slicing.

I know [::-1] reverses a string. i want to know what value it assign to start and stop to get a reverse.

i thought it would be 0 to end to string. and tried

f="foobar"

f[0:5:-1]---> it gives me no output. why?

and i have read start should not pass stop. is that true in case of negative step value also?

can anyone help me to clear my doubt.

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
shubendrak
  • 2,038
  • 4
  • 27
  • 56

2 Answers2

4

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.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • in the case f[5::-1], what value it assign to stop. is it -1 – shubendrak Sep 27 '13 at 07:06
  • Since the value for stop is missing, it's assigned the value `None`. Therefore, when you do `f[:]`, that's actually a shortcut for doing `f[None:None]`. Admittedly, this is a little confusing at first glance, but it does make a sort of sense. If the stop value is `-1`, then it means you want to slice up to but not including the last char. You need some value to indicate "slice to the end", and no number really makes sense in that context. – Michael0x2a Sep 27 '13 at 07:10
  • oh, i thought f[:] means f[0:end-of-string]. thanks a ton for clearing my doubt – shubendrak Sep 27 '13 at 07:20
  • @Shubendra -- technically, that's correct. Doing `f[:]`, `f[0:]`, `f[0:None]`, and `f[None:None]` will return the exact same thing. However, since there's no `end-of-string` value in Python, they decided to settle on using `None`. To keep things consistent, the Python devs settled on making `None` work for the `start` value as well, alongside 0. – Michael0x2a Sep 27 '13 at 07:23
1

You can think like that:

with f[0:5], 0 is the start position and 5-1 the end position.

with f[0:5:-1], 5 is the start position and 0+1 the end position.

In slicing the start position must be lower than the end position. When this is not the case the result is an empty string. Thus f[0:5:-1] returns an empty string.

user1813
  • 97
  • 2
  • 11
  • No! you always go from the first operand to the left of the colon :, i.e `[start:stop:step]`, so in your case with `f[0:5:-1]` **5** is **NOT THE START POSITION**... the first item in the `f[0:5:-1]` would be the first _index (0) - (step)_, i.e _0 - 1 = -1_ , second item would be _index (0) - (step + 1)_, i.e _0 - 2 = -2_, and so on... it goes backwards..read the answered question at the top of this question, there's a link there. – Marius Mucenicu Apr 18 '18 at 05:59