[Python] Python why would you use [:] over =

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

  1. Stack

    Stack Membro Participativo

    I am just learning python and I am going though the tutorials on https://developers.google.com/edu/python/strings

    Under the String Slices section


    s[:] is 'Hello' -- omitting both always gives us a copy of the whole thing (this is the pythonic way to copy a sequence like a string or list)
    ​

    Out of curiosity why wouldn't you just use an = operator?

    s = 'hello';
    bar = s[:]
    foo = s


    As far as I can tell both bar and foo have the same value.

    Continue reading...

Compartilhe esta Página