What is a Pythonic way to pad a numeric string with zeroes to the left, i.e. so the numeric string has a specific length?
str(n).zfill(width) will work with strings, ints, floats... and is Python 2.x and 3.x compatible:
str(n).zfill(width)
string
int
float
>>> n = 3 >>> str(n).zfill(5) '00003' >>> n = '3' >>> str(n).zfill(5) '00003' >>> n = '3.0' >>> str(n).zfill(5) '003.0'