ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
s = 'a-b-c'
print(s.split('-', 1), s.rsplit('-', 1)[-1], s.partition('-')[2])
  1. A. ['a', 'b'] c b-c
  2. B. ['a', 'b-c'] c b-c
  3. C. ['a', 'b', 'c'] c b
  4. D. ['a', 'b-c'] b d

Correct answer: B. ['a', 'b-c'] c b-c

split with maxsplit=1 cuts at the first hyphen only. rsplit with maxsplit=1 cuts at the last hyphen, so the final piece is c. partition gives everything after the first hyphen.

More Python Programming questions

Exams that ask this topic