ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
s = 'abcdef'
print(s[-2:], s[:-2], s[-10:], s[::-2][0])
  1. A. ef abcd abcde f
  2. B. fe abcd abcdef f
  3. C. ef abcd abcdef e
  4. D. ef abcd abcdef f

Correct answer: D. ef abcd abcdef f

s[-2:] is ef, and s[:-2] drops the last two characters to give abcd. A start index far before the beginning clamps to the whole string, and reversing with step -2 begins at f.

More Python Programming questions

Exams that ask this topic