Predict the output
What does this print?
word = "abba"
print([word[i:j] for i in range(len(word))
for j in range(i + 2, len(word) + 1)
if word[i:j] == word[i:j][::-1]])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A ['abba', 'bb']
Right: the only longer mirror slices are the whole word and the middle pair.
Those two slices plus single letters give the three cuttings of 'abba': four singles, 'a bb a', and the whole word.
Why not the others?
- B ['bb']. The whole word also reads the same backwards.
- C ['a', 'b', 'b', 'a', 'abba', 'bb']. Single letters are excluded by starting j at i + 2.