on slices, negative indices, which are the equivalent procedures?

Chris Angelico rosuav at gmail.com
Fri Aug 6 18:58:49 EDT 2021


On Sat, Aug 7, 2021 at 5:22 AM Boris Dorestand <bdorestand at example.com> wrote:
>
> Jach Feng <jfong at ms4.hinet.net> writes:
>
> >> >>>>> 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?
> > That's not possible because a positive index is relative to the leftmost item 0
>
> And the middle index is always exclusive, so we can't go to the left of
> 0 and remain positive.  Okay, I think that answers it.  It's not
> possible at all.
>

An absent index isn't the same as any specific positive value, so,
yeah, it's not possible to replace it with a positive index. It IS
possible to replace it with None.

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

You could implement equivalent logic in your function.

ChrisA


More information about the Python-list mailing list