ProgrammingModerateAccenture · Capgemini pattern

What is printed?

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

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

The second argument is the maximum number of splits, not the number of pieces. split("-", 2) splits twice from the left giving three pieces with the remainder "c-d" kept whole. rsplit("-", 1) splits once from the right: ['a-b-c', 'd'].

More Python Programming questions

Exams that ask this topic