How to input a number from 0 to 1000 and have python return a string that is always 4 characters long. for example: input 10 python should return 0010
Asked
Active
Viewed 1,618 times
1
-
OR: `str(10).zfill(4)` – U13-Forward Aug 18 '18 at 07:23
2 Answers
2
You can use zero padding format with fixed width:
"{:04d}".format(number)

taras
- 6,566
- 10
- 39
- 50
-
-
It makes a string from a number in the fixed width format (4 characters) and uses zeros rather than spaces for padding. – taras Aug 18 '18 at 06:41
0
You can do something like this, Hope this helps
num = 10
if num < 1000:
print "%04d" % (num,)

Sachi.Dila
- 1,126
- 7
- 15