-3
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:
GMuhammed
  • 85
  • 8
  • You need to assign the result of `replace()` to a variable. Strings are immutable, so it can't modify in place. – Barmar Aug 09 '21 at 22:09

1 Answers1

2

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.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30