Python auto complement 0

There are two ways to format integers

1. Use the python built-in method zfill()
2. Use formatted printing %

zfill()

zfill is a built-in method of strings, and the caller is a string instance.
zfill does not change the original string object (immutable type), and generates a new string to return

>>str1 = "10"
>>print(str1.zfill(5))
00010
>>print(str1)
10

Format%

>>> a = 5
>>> b = "%05d" %(a)
>>> print(b)
00005
>>> print(a)
5

Using% formatting, you can add zeros to pure numbers without changing the original variables.

Guess you like

Origin blog.csdn.net/qq_34626094/article/details/112979237