1

I cannot write s[::-1] which reverses the string s using explicit indices. I want to write s[a:b:-1] but I don't know what to put for a and b. Here is a code sample, it should produce fedcba:

s = 'abcdef'
s[::-1]

From the documentation:

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

s5s
  • 11,159
  • 21
  • 74
  • 121
  • Please explain how should be the output? – NAGA RAJ S Jun 24 '20 at 13:10
  • 1
    I don't understand what you are asking. Why "can't" you write `s[::-1]`? You *can* write it and it produces the output you want. – mkrieger1 Jun 24 '20 at 13:11
  • "I don't know what to put for a and b" - what did you *try* to put and what was the problem with it? – mkrieger1 Jun 24 '20 at 13:12
  • 3
    @mkrieger1 I can - I just want to understand how I can write this using explicit indices. It's a valid question - I should be able to write this using `s[a:b:-1]` where a,b are some expressions. – s5s Jun 24 '20 at 13:12
  • ...so, it's a curiosity question, not a practical problem? Our rules require that problems be _practical_ -- that their answers change how you go about the practice of programming. (See https://meta.stackoverflow.com/questions/297058/when-is-asking-questions-out-of-curiosity-off-topic on [meta]) – Charles Duffy Jun 24 '20 at 13:13
  • 1
    `s[len(s):None:-1]` seems to work – sdcbr Jun 24 '20 at 13:14
  • @Charles You could contrive a situation where you dynamically decide values for `a, b, c` to then pass to `s[a:b:c]`… – deceze Jun 24 '20 at 13:15
  • 1
    @deceze, sure; such a scenario is given (with a specific attempt at a working implementation and a specific failure mode encountered), we have a practical question. f/e, not having a _practical_ failure, we can't say whether `None` is an adequate answer in the `b` field, or if it really needs to be `0` (or another non-negative integer value); because the only basis on which an answer can be determined to be correct is the entirely subjective "does the OP consider their curiosity resolved?". – Charles Duffy Jun 24 '20 at 13:16
  • 2
    Here's another one that hasn't been listed yet: ``s[-1:-len(s)-1:-1]``, though ``s[None:None:-1]`` is probably more readable. – Mike Scotty Jun 24 '20 at 13:21
  • Would you not then say that the python documentation is wrong? Clearly, omitted indices default to `None`. If one experiments with slicing `s` it is clear the default is `None` if an index is omitted. – s5s Jun 24 '20 at 13:21
  • 2
    I think this might boil down to this question: https://stackoverflow.com/questions/399067/extended-slice-that-goes-to-beginning-of-sequence-with-negative-stride which essentially asks "why does using -1 as `stop` value not work as expected". Or this: https://stackoverflow.com/questions/5798136/python-reverse-stride-slicing – mkrieger1 Jun 24 '20 at 13:22
  • @s5s, ...I would say that the Python documentation about default behavior doesn't cover the negative-step case, yes. OTOH, the negative-step case doesn't _happen_ in the presence of purely default behavior. :) – Charles Duffy Jun 24 '20 at 13:24
  • @CharlesDuffy But even with non-negative step size, Using `None` in place of omitted indices produces consistent results. For example, `s[None, None]` or `s[None:3]` etc. It appears, it is more correct to suggest that the default is `None` if an index is omitted. I haven't looked at the implementation but I would imagine that it defaults to `None`. – s5s Jun 24 '20 at 13:28
  • I wouldn't be surprised if the implementation recognized `None` as a placeholder, and replaced it with something else (such as a non-negative index with a specific numeric value) later. – Charles Duffy Jun 24 '20 at 14:14

4 Answers4

2

If you really want to fill all 3 placeholders you can do

s[len(s):None:-1]

Edit: as suggested by @deceze, this works too

s[None:None:-1]
Pani
  • 1,317
  • 1
  • 14
  • 20
  • 3
    `None` for the start would do fine too… – deceze Jun 24 '20 at 13:14
  • 1
    Well, this is what happens when you left it empty, it becomes `None` and Python handles it. So I think this is still not an answer. – Asocia Jun 24 '20 at 13:16
  • 1
    @Asocia If the question is to find values for `a, b, c` that will replicate the behaviour of `s[::-1]` with `s[a:b:c]`, then surely `None` is a valid solution. – deceze Jun 24 '20 at 13:18
  • 1
    Also, omitted indices default to zero or to the size of the string, at least as stated in the question. – sdcbr Jun 24 '20 at 13:18
  • @deceze Then why @Pani answered `s[len(s):None:-1]` but not `s[None:None:-1]`? They tried to find `a` and `len(s)` was a valid value for `a`, tried to find `b` but couldn't find and put `None`. Revealing the obvious, `None`, shouldn't be considered as answer. – Asocia Jun 24 '20 at 13:26
0

I want to write s[a:b:-1] but I don't know what to put for a and b.

There you go:

>>> s = 'abcdef'
>>> a =  len(s) - 1 
>>> b = -len(s) - 1 
>>> s[a:b:-1]
'fedcba'
Asocia
  • 5,935
  • 2
  • 21
  • 46
0

Here are values for a and b that work:

lst = list('abcd')

a = b = None
print (lst[a:b:-1]) # returns ['d', 'c', 'b', 'a']

a = len(lst)
b = 0 - len(lst) - 1 # b = -5

print (lst[a:b:-1]) # returns ['d', 'c', 'b', 'a']
-1

this should do it.

s[len(s)::-1]
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33