English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Python string zfill() usage and example
zfill() method returns a copy of the string, the left side of which is filled with '0' characters.
The syntax of zfill() in Python is:
zfill() parameter
zfill() uses a single character width.
zfill() returns value
。In this case, zfill() returns a copy of the string, the left side of which is filled with five '0' digits.10,and the width it specifies will be15zfill() returns a copy of the string, the left side of which is filled with '0'. The length of the returned string depends on the provided width.
。In this case, zfill() returns a copy of the string, the left side of which is filled with five '0' digits.10,and the width it specifies will be8。In this case, zfill() does not fill '0' on the left and returns a copy of the original string. In this case, the length of the returned string will be10。
text = "program is fun" print(text.zfill(15)) print(text.zfill(20)) print(text.zfill(10))
When running the program, the output is:
0program is fun 000000program is fun program is fun
If the string starts with a symbol prefix (+,'-Starting with '),then '0' digits are filled before the first prefix character.
number = "-290" print(number.zfill(8)) number = "+290" print(number.zfill(8)) text = "--random+text" print(text.zfill(20))
When running the program, the output is:
-0000290 +0000290 -0000000-random+text