What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".
I can think of a couple ways, but I want to see if someone comes up with something slick.
int('00000001') + 1
if you want the leading zeroes back:
"%08g" % (int('000000001') + 1)
Use the much overlooked str.zfill():
str(int(x) + 1).zfill(len(x))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With