I'm trying to understand the replace method. I have a string of numbers where I would like to make some adjustments. Particularly, I would categorize the numbers based on a threshold:
def makeAdjustment(x):
for each in x:
if int(each) < 5:
x = x.replace(each, "0")
else:
x = x.replace(each, "1")
return x
In use:
>>> makeAdjustment("800857237867") == "100111001111"
True
>>> makeAdjustment("15889923") == "01111100"
True
>>> makeAdjustment("14963896") == "00110111"
True
However, if the sequence of numbers gets larger the string is converted to zero:
>>> makeAdjustment("366058562030849490134388085")
'000000000000000000000000000'
replacemethod returns a copy of the string with all occurrences of substring old replaced by new. So, eventually everything is becoming zero.xfor each step.