Display number with leading zeros

南楼画角 提交于 2019-11-25 22:38:51

问题


Given:

a = 1
b = 10
c = 100

How do I display a leading zero for all numbers with less than two digits?

That is,

01
10
100

回答1:


In Python 2 you can do:

print "%02d" % (1,)

Basically % is like printf or sprintf.


For Python 3.+ the same behavior can be achieved with:

print("{:02d}".format(1))

For Python 3.6+ the same behavior can be achieved with f-strings:

print(f"{1:02d}")



回答2:


You can use str.zfill:

print str(1).zfill(2) 
print str(10).zfill(2) 
print str(100).zfill(2) 

prints:

01
10
100



回答3:


In Python 2.6+ and 3.0+, you would use the format() string method:

for i in (1, 10, 100):
    print('{num:02d}'.format(num=i))

or using the built-in (for a single number):

print(format(i, '02d'))

See the PEP-3101 documentation for the new formatting functions.




回答4:


print('{:02}'.format(1))
print('{:02}'.format(10))
print('{:02}'.format(100))

prints:

01
10
100



回答5:


Or this:

print '{0:02d}'.format(1)




回答6:


In Python >= 3.6, you can do this succinctly with the new f-strings that were introduced by using:

f'{val:02}'

which prints the variable with name val with a fill value of 0 and a width of 2.

For your specific example you can do this nicely in a loop:

a, b, c = 1, 10, 100
for val in [a, b, c]:
    print(f'{val:02}')

which prints:

01 
10
100

For more information on f-strings, take a look at PEP 498 where they were introduced.




回答7:


x = [1, 10, 100]
for i in x:
    print '%02d' % i

results in:

01
10
100

Read more information about string formatting using % in the documentation.




回答8:


The Pythonic way to do this:

str(number).rjust(string_width, fill_char)

This way, the original string is returned unchanged if its length is greater than string_width. Example:

a = [1, 10, 100]
for num in a:
    print str(num).rjust(2, '0')

Results:

01
10
100



回答9:


Or another solution.

"{:0>2}".format(number)



回答10:


Use a format string - http://docs.python.org/lib/typesseq-strings.html

For example:

python -c 'print "%(num)02d" % {"num":5}'



回答11:


This is how I do it:

str(1).zfill(len(str(total)))

Basically zfill takes the number of leading zeros you want to add, so it's easy to take the biggest number, turn it into a string and get the length, like this:

Python 3.6.5 (default, May 11 2018, 04:00:52) 
[GCC 8.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> total = 100
>>> print(str(1).zfill(len(str(total))))
001
>>> total = 1000
>>> print(str(1).zfill(len(str(total))))
0001
>>> total = 10000
>>> print(str(1).zfill(len(str(total))))
00001
>>> 



回答12:


width = 5
num = 3
formatted = (width - len(str(num))) * "0" + str(num)
print formatted



回答13:


Use:

'00'[len(str(i)):] + str(i)

Or with the math module:

import math
'00'[math.ceil(math.log(i, 10)):] + str(i)



回答14:


df['Col1']=df['Col1'].apply(lambda x: '{0:0>5}'.format(x)) (the 5 is the number of total digits)

I used this link: http://www.datasciencemadesimple.com/add-leading-preceding-zeros-python/




回答15:


If dealing with numbers that are either one or two digits:

'0'+str(number)[-2:] or '0{0}'.format(number)[-2:]



来源:https://stackoverflow.com/questions/134934/display-number-with-leading-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!