[Python] Possible string slicing bug [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 24, 2025.

  1. Stack

    Stack Membro Participativo

    Let's create next variable:

    weather = 'light raining'


    Now let's try to print next part of string:

    print(weather[-1:4])


    Result is empty string. As expected.

    Now let's try to print this string:

    print(weather[-1:])


    Expected: empty string

    Got: 'g'

    Note: same behavior gives us print(weather[-1]). However [-1] is expected to return 'g', while [-1:] is expected to return empty string, same as [-1:4].

    If it's not a bug but feature, could somebody please explain me how starting position can be minus? And why in one case ([-1:4]) it's work as expected returns empty string, while in another ([-1:]) it returns value?

    CPython version tested on: 3.13

    Operating system tested on: Windows 11

    Continue reading...

Compartilhe esta Página