ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
s = 'abc'
print(s[::-1] + s[0], s[1:] + s[:1], s[-1] + s[-2:])
  1. A. cbaa bca cbb
  2. B. cbaa acb cbc
  3. C. cbaa bca cbc
  4. D. cba bca cbc

Correct answer: C. cbaa bca cbc

s[::-1] is cba and s[0] is a, giving cbaa. Rotating left moves a to the end, giving bca, and s[-2:] is bc, so the last value is cbc.

More Python Programming questions

Exams that ask this topic