ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print('a-b_c d'.split('-'), 'a-b_c d'.replace('-', ' ').split('_'), 'a-b'.partition('-')[0])
  1. A. ['a', 'b'] ['a b', 'c d'] a
  2. B. ['a', 'b_c d'] ['a b', 'c d'] a
  3. C. ['a', 'b_c d'] ['a', 'b c'] a
  4. D. ['a', 'b_c d'] ['a b', 'c d'] b

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

split('-') breaks at the hyphen only, so the underscore stays inside the second piece. Replacing hyphens with spaces and then splitting on the underscore gives two pieces, and partition returns the text before the first hyphen.

More Python Programming questions

Exams that ask this topic