ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

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

split(',') keeps empty fields, so the trailing comma leaves an empty last item. split(' ') with an explicit space keeps the empty fields between the double space and at both ends, unlike split() with no argument.

More Python Programming questions

Exams that ask this topic