ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

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

split() with no argument splits on any whitespace, so the first list has four items. The second replace turns only the commas into spaces, so the semicolon stays inside one piece, and split(' ') breaks only on single spaces.

More Python Programming questions

Exams that ask this topic