-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
  • replace returns a new string. Commented Jul 15, 2020 at 19:23

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.