-3

I was testing some mechanics out and ran into an issue, the following program should replace the '+' sight to ' + '. The output of this theoretically should be '20 + 20', but in reality, it's '20+20'. I have no idea why.

string = "20+20"
if string.find(" ") == -1:
    string.replace("+", " + ")
print(string)

1 Answers1

2

In order for this to work, you need to reassign the string variable with the result of string.replace as the replace function returns the new string.

string = "20+20"
if string.find(" ") == -1:
    string = string.replace("+", " + ")
print(string)
Harben
  • 1,802
  • 1
  • 11
  • 16