ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
x = 'a,b;c'
print(x.split(',', 1), x.split(';')[0].split(','), [t for t in x.replace(';', ',').split(',') if t], x.count(',') + x.count(';'))
  1. A. ['a', 'b', 'c'] ['a'] ['a', 'b;c'] 1
  2. B. ['a', 'b;c'] ['a'] ['a', 'b', 'c'] 2
  3. C. ['a', 'b;c'] ['a', 'b'] ['a', 'b', 'c'] 1
  4. D. ['a', 'b;c'] ['a', 'b'] ['a', 'b', 'c'] 2

Correct answer: D. ['a', 'b;c'] ['a', 'b'] ['a', 'b', 'c'] 2

split with maxsplit 1 stops after the first comma, so the rest stays together, including the semicolon. Splitting on the semicolon first leaves a,b, and replacing ; with , before splitting gives three items. The two counts add up to 2.

More Python Programming questions

Exams that ask this topic