ProgrammingHardTCS NQT · Infosys

What is printed?

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

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

split with an explicit separator keeps empty fields, so 'a,b,,c' gives an empty string. split() with no argument collapses runs of whitespace, but split(' ') on 'a b c' keeps the empty field between the two spaces. 'x,' splits into ['x', ''].

More Python Programming questions

Exams that ask this topic