Predict the output
What does this print?
text = "xabbay"
lo, hi = 2, 3
while 0 <= lo and hi < len(text) and text[lo] == text[hi]:
lo, hi = lo - 1, hi + 1
print(text[lo + 1:hi], lo, hi)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A abba 0 5
Right: expansion stops when x and y differ, one step past the match.
The loop always ends one step beyond the last match, which is why the slice starts at lo + 1.
Why not the others?
- B abba 1 4. The loop moves lo and hi once more before the failing check.
- C bb 1 4. a and a also match around the bb centre.