Python字符串 | zfill()
zfill()方法返回字符串的副本,其中 '0'字符填充到给定字符串的左侧。
句法 :
str.zfill(length)
参数 :
length : length is the length of the returned string from zfill() with ‘0’ digits filled to the leftside.
返回 :
Returns a copy of the string with '0' characters
padded to the leftside of the given string.
代码 1
text = "geeks for geeks"
print(text.zfill(25))
print(text.zfill(20))
# Given length is less than
# the length od original string
print(text.zfill(10))
输出 :
0000000000geeks for geeks
00000geeks for geeks
geeks for geeks
代码 2
number = "6041"
print(number.zfill(8))
number = "+6041"
print(number.zfill(8))
text = "--anything%(&%(%)*^"
print(text.zfill(20))
输出 :
00006041
+0006041
-0-anything%(&%(%)*^