Can you display an integer value with leading zeroes using the str.format function?
Example input:
"{0:some_format_specifying_width_3}".format(1)
"{0:some_format_specifying_width_3}".format(10)
"{0:some_format_specifying_width_3}".format(100)
Desired output:
"001"
"010"
"100"
I know that both zfill and %-based formatting (e.g. '%03d' % 5) can accomplish this. However, I would like a solution that uses str.format in order to keep my code clean and consistent (I'm also formatting the string with datetime attributes) and also to expand my knowledge of the Format Specification Mini-Language.
max_width,a,b, andcvariable names or is your input a string? - Andrew Clarkmax_widthwas used for the example to indicate how many leading zeroes would be necessary for the example values. In my actual code the input to format includes a datetime object and some ints that I would like to format with leading zeroes. - butchformator a function that uses nothing but raw python would suffice? - Ryan Saxeformat. - butch