.replace(<string>)
returns a modified string. Normally, we could set it equal to a new variable. But the programmer chose to overwrite the old variable x
with the new modified string.
It is similar to saying x = x + 1
. You are modifying the old string, but instead of saving it to a new variable, the programmer chose to update the variable.
You do not have to always set a variable, but if the function returns a value it is recommended (or append it to a list or something). What is the point of calling a function if you are not going to set the return to something?
You don't have to if you are using it in a conditional. For example (this could be done in a simpler way, but FOR THE PURPOSE OF THE EXAMPLE):
x = raw_input("Please input a number\n")
The number is then stored as x.
If you want to compare the number to another value, you can call the function int()
within the conditional instead of setting it as another variable.
if int(x) == 5:
print 'Yay, you guessed right!'
The alternative:
y = int(x)
if y == 5:
print 'Yay, you guessed right!'
Hope this helps!
Happy coding, and best of luck!