There are a number of existing posts on this topic, but I cannot get this simple piece of code to work correctly. I have tried many many times with no success...
per = 5
if per < 10 == True:
ms = 'fwd_%sd' % str(per).zfill(2)
else:
ms = 'fwd_%sd' % per
and
if per < 10 == True:
a = format(per, 'fwd_02d')
else:
a = 'fwd_%sd' % per
The result should be 'fwd_05d' but I can only get 'fwd_5d'.
Among others, I have seen:
Display number with leading zeros
Please help!
You can use str.format, if you are actually doing nothing else but padding just use
ms = 'fwd_{:02}d'.format(per)
and forget the if/else, only numbers less than 10 will be padded with a 0. The 0 in{:02}
is what to pad with and the 2 is the size to pad to.On another note
if per < 10 == True:
is the same asif per < 10: