Python – how to restore original string after replace? [closed]

You already have 4 instances of 9F in your original string. Those are causing the wierd behaviour

a="DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83"

print(a.count('9F'))
#4

Otherwise the string.replace works perfectly

a="hello"
b = a.replace('l','a')
print(b)
#heaao
c = b.replace('a','l')
print(c)
#hello
print( a == c)
#True

Gives you True (Convert l to a, and then a to l)

Possible solution, replace existing 9F to something not present in the string,say XY, then use it in replacing back as well

a="DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83"

#Replace 9F to XY, then D6 to 9F
b = a.replace('9F','XY').replace('D6','9F')
#Replace 9F to D6, then XY to 9F
c = b.replace('9F', 'D6').replace('XY', '9F')

print(a == c)
#True

Leave a Comment