ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print('x'.removeprefix('xx'), 'hello'.removesuffix('llo'), 'abc'.removeprefix(''))
  1. A. x hello abc
  2. B. x hel abc
  3. C. x he ab
  4. D. x he abc

Correct answer: D. x he abc

removeprefix and removesuffix remove the text only when it matches exactly at that end, so 'x' does not start with 'xx' and stays x. 'hello' loses its trailing 'llo', leaving 'he'. This differs from strip(), which removes any characters from a set.

More Python Programming questions

Exams that ask this topic