ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 'aXbX'
print(x.split('X'), x.partition('X')[0] + x.rpartition('X')[2])
  1. A. ['aX', 'bX'] ab
  2. B. ['a', 'b', ''] a
  3. C. ['a', 'b'] ab
  4. D. ['a', 'b', ''] aX

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

split('X') keeps the empty string after the final X. partition('X')[0] is 'a', and rpartition('X')[2] is the empty text after the last X, so the concatenation is 'a'.

More Python Programming questions

Exams that ask this topic