79545634

Date: 2025-03-31 06:50:29
Score: 3
Natty:
Report link

In python3,

  1. Strings are immutable sequences of Unicode code points.
  2. A slice of Sequence Types is defined as the sequence of items with the indexes.
  3. Sequences of the same type are compared lexicographically by comparing corresponding elements.

That means string_a[i:i + k] == string_b[j:j + k] is comparing two new sliced sequence by comparing elements in them, while not creating new string but new sequence.

does Python optimize this?

It is hard to say it inefficient or not without any of you code.

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

No.

Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: vassiliev