ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
s = slice(1, None, 2)
t = "abcdefg"
print(t[s], s.indices(len(t)), t[::-1][s])
  1. A. bdf (1, 7, 2) fdb
  2. B. bdf (1, None, 2) fdb
  3. C. bdf (1, 7, 2) gec
  4. D. bdf (1, 6, 2) fdb

Correct answer: A. bdf (1, 7, 2) fdb

slice(1, None, 2) picks indices 1, 3, 5 = 'bdf'. indices(7) resolves None to the length, giving (1, 7, 2). Reversed, the string is 'gfedcba', whose indices 1, 3, 5 are 'f', 'd', 'b'.

More Python Programming questions

Exams that ask this topic