ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 'abc'
print(x.replace('', '-'))
  1. A. -abc-
  2. B. -a-b-d-
  3. C. -a-b-c-
  4. D. a-b-e

Correct answer: C. -a-b-c-

An empty search string matches at every position, including both ends, so the replacement is inserted before each character and at the end. This is a classic trap for string-processing solutions.

More Python Programming questions

Exams that ask this topic