ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
s = 'abcdefg'
print(s[::-2], s[5:1:-2], s[-3::-3])
  1. A. geca fd be
  2. B. gdba fd eb
  3. C. geca df eb
  4. D. geca fd eb

Correct answer: D. geca fd eb

A negative step walks backwards from the start index. s[::-2] takes g, e, c, a, s[5:1:-2] takes f and d, and s[-3::-3] starts at e and then reaches b.

More Python Programming questions

Exams that ask this topic