I have the following function:
def troca(s):
letra1 = s[0]
letra2 = s[-1]
s = s.replace(s[0], letra2)
s = s.replace(s[-1], letra1)
return s
troca ('bar')
As you can see, I provide as an argument to my function troca
to string 'bar'
. The result that I expect the function to return is 'rab'
(that is, the first letter changed by the last one), but when I run the code, I have 'bab'
as its return.
In the python tutor, I see that the code runs normally up to the second replace
. in the first replace
, my string is 'rar'
(so far so good, I changed the first letter of the string to the last one), but in the second, my string simply gets 'bab'
, that is, it changes the first one and the last letter, all for the first. Why does it happen? Thank you.