[Python] Index reference by None in Python [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently trying out slicing a string in Python and was confused about how exactly does Python interpret the term 'None' because it behaves rather conveniently when used but I am not sure of the process that goes on in the interpreter when we use it.

    I have no doubts about how to use slice, but about HOW it works. None of the questions I have visited (for example) explain what index does 'none' refer to for each case mentioned below.

    I got this question because I was playing around with negative indexing in Python;

    str="Python"
    print(str[-len(str):-1])


    Will give us the output; Pytho

    If we try replacing the -1 with a 0 it obviously returns an empty string. However if we replace it with None, it will return the whole string

    str="Python"
    print(str[-len(str):])


    Will give us the output Python

    Even when we do this

    str="Python"
    print(str[:])


    It will return the whole string!

    I was wondering what Index does the 'None' refer to in each case above?

    Continue reading...

Compartilhe esta Página