ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(' a b '.split(' '), ' a b '.split(), 'a	b x c d'.split(None, 1))
  1. A. ['', 'a', 'b', ''] ['a', 'b'] ['a', 'b x c d']
  2. B. ['', 'a', 'b', 'x'] ['a', 'b'] ['a', 'b x c d']
  3. C. ['', 'a', 'b', ''] ['a', 'b'] ['a', 'b', 'c d']
  4. D. ['', 'a', 'b'] ['a', 'b'] ['a', 'b c']

Correct answer: A. ['', 'a', 'b', ''] ['a', 'b'] ['a', 'b x c d']

split(' ') keeps an empty field at each end and between the separators, but split() with no argument drops empty fields. With maxsplit 1, the rest of the string after the first run of whitespace is kept intact, so 'b x c d' stays as one piece.

More Python Programming questions

Exams that ask this topic