Adding leading zeros to a numeric value and converting it to a string

Assuming you want to pad the zeroes in front, here’s a few different ways.

strValue = '%010d' % value

strValue = '{:010d}'.format(value)

strValue = str(value).zfill(10)

The second one, I believe, is considered the preferred method for Python 2.7+, but a few options doesn’t hurt.

2 Likes