[Python] Does comparing the substring of two strings create new strings in Python? [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    If I do string_a[i:i + k] == string_b[j:j + k], is this inefficient because slicing creates a new string because strings are immutable? Or does Python optimize this?

    Is it possible that using a for loop and comparing character by character could be more efficient?

    I'm not sure that stackoverflow.com/questions/49950747/… is a duplicate because the answers there do not mention slicing a string using [] and then comparing. I already looked at that question before, but still wasn't sure about the answer.

    Continue reading...

Compartilhe esta Página