How do you format a number as a string so that it takes a number of spaces in front of it? I want the shorter number 5 to have enough spaces in front of it so that the spaces plus the 5 have the same length as 52500. The procedure below works, but is there a built in way to do this?
a = str(52500) b = str(5) lengthDiff = len(a) - len(b) formatted = '%s/%s' % (' '*lengthDiff + b, a) # formatted looks like:' 5/52500'
Format operator:
Using
*spec, the field length can be an argument: