Filling 0 before string

Asked 2 years ago, Updated 2 years ago, 41 views

I'm going to print out a number, but I want to make it 5 digits no matter what. For example,

print(3) = 00003
print(50000) = 50000
print(723) = 00723

Like this! How can I make it short?

string python

2022-09-21 18:17

1 Answers

Fill in the front with 0 to adjust the string length to width

"3".zfill(5)
"50000".zfill(5)
"723".zfill(723)

Fill in the front with fillchar to set the string length to width

"5".rjust(5, '0')
print ("%05d"% 5)
print (format (5000,05')) #2.6 or higher
print ('{0:05d}'.format (273) #2.6 or later


2022-09-21 18:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.