Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 26, 2011

padding a number with zeroes

# beautiful and fast (if you know in advance how many to pad)
>>> "%03d" % 2 
'002'

>>> "%04d" % 22 
'0022'
for more information read the format-specification-mini-language. Thanks Sanne!

an alternative offered by nfloyd is
# if you only know at runtime how much to pad
>>> i = 4
>>> str(i).zfill(3)
'004'
or
>>> '{:07d}'.format(20)
'0000020'

No comments:

Post a Comment

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.