On 05/08/2021 12.35, Jack Brandom wrote:
The FAQ at

   https://docs.python.org/3/faq/programming.html#what-s-a-negative-index

makes me think that I can always replace negative indices with positive
ones --- even in slices, although the FAQ seems not to say anything
about slices.

With slices, it doesn't seem to always work.  For instance, I can
reverse a "Jack" this way:

s = "Jack Brandom"
s[3 : -13 : -1]
'kcaJ'

I have no idea how to replace that -13 with a positive index.  Is it
possible at all?

I don't think so, because the second number (in this case -13) is the
index before which you stop. For example:

>>> s
'Jack Brandom'
>>> s[3:0:-1]
'kca'
>>> s[3:1:-1]
'kc'
>>>

However, since you want to go through item 0 in the original string,
you don't need a number there at all:

>>> s[3::-1]
'kcaJ'
>>>

Or, it you want to be more explicit, you could separately grab the
substring and then reverse it:

>>> s[:4][::-1]
'kcaJ'
>>>

Does any of this help?

--
Michael F. Stemper
If it isn't running programs and it isn't fusing atoms, it's just bending space.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to