Python Strings · Question 9 · Medium

replace() is called on a string and both the old and new names are printed. What is the output?

s = "mango"
t = s.replace("m", "t")
print(s, t)
  1. tango tango
  2. mango mango
  3. mango tango
  4. tango mango
Answer and explanation

Answer: C. mango tango

Explanation: Strings are immutable, so replace() cannot change s; it returns a new string. s is still "mango" and t is the new string "tango".

Previous: A slice with a step of 3 is taken from a city name. What does it print? · Next: split() is called once with a comma separator and once with no argument. What…

Practise python strings

Try a fresh set of python strings questions with an explanation after every answer, free and without signing up: Python Strings practice. Or sit a full placement mock test.

More python strings questions

More python practice

Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge