str relace method confusion

Question:

x=["a = 1;","b = input();","if a + b > 0 && a - b < 0:"]
for i in range(len(x)):
    y = x[i]
    y.replace(" && ", " and ")
    y.replace(" || ", " or ")
    print(y)

This code is suppose to print

a = 1; 
b = input(); 
if a + b > 0 and a - b < 0:

but I am getting result which corresponds to replace method not working. I have tried this on colab as well as jupiter notebook.

a = 1; 
b = input();
if a + b > 0 && a - b < 0:
Asked By: Gbadegesin

||

Answers:

Strings in Python cannot be modified. The replace function returns a new string with the change.

y = y.replace(" && ", " and ")
y = y.replace(" || ", " or ")

Which can, of course, be combined into one statement.

Answered By: Tim Roberts
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.