Data Structures & AlgorithmsModerateWipro · Accenture pattern

What is printed? (Python string immutability and methods)

What is printed? (Python string immutability and methods)
s = "Hello World"
t = s.replace("o", "0", 1)
u = s.swapcase()[::2]
print(s, t, u)
  1. A. Hell0 World Hell0 World hLOwRD
  2. B. Hello World Hell0 W0rld hLOwRD
  3. C. Hello World Hell0 World hLOwRD
  4. D. Hello World Hell0 World hLO RD

Correct answer: C. Hello World Hell0 World hLOwRD

Strings are immutable, so s is unchanged. replace(..., 1) replaces only the first 'o'. swapcase gives "hELLO wORLD" and [::2] takes indices 0, 2, 4, …: h, L, O, w, R, D.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic