0

How would you format '7' to print '007' here:

>>> number = 7
>>> print(number)
7

Ideally, there'd something similar to always showing these decimal places?:

>>> number = 7
>>> print("{:.2f}".format(number))
7.00

I've searched online for a similar solution to this but can't seem to find anything in this format...

Thanks for the help!

Caleb Mackle
  • 120
  • 1
  • 6

2 Answers2

1

You just need to cast 7 using str() and finally use zfill to add leading zeros:

Return a copy of the string left filled with ASCII '0' digits to make a string of length width. A leading sign prefix ('+'/'-') is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s).


str(7).zfill(3)
>>> '007'
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

try this

print("{0:0=3d}".format(number))
Lavanya V
  • 337
  • 2
  • 7