Python format Integer into fixed length strings [duplicate]

Use the format() function or the str.format() method to format integers with zero-padding:

print format(integervalue, '05d')
print 'Formatted String : {0:05d}'.format(integervalue)

See the Format Specification Mini-Language; the leading 0 in the format signifies 0-padding, the 5 is the minimal field width; any number shorter than that is padded to the full width.

Demo:

>>> format(110, '05d')
'00110'
>>> 'Formatted String : {0:05d}'.format(12)
'Formatted String : 00012'

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)