python字符串操作--zfill()方法用0填充前缀

返回长度为 width 的新字符串,原字符串右居,使用ASCII 0 数码填充左边的空位。如果有正负值(+-)前缀,则 0 填充在正负值之后

格式及参数

1
str.zfill(width)

str : 字符串对象

width : 新字符串的总宽度,小于等于 len(str) 时则返回原字符串

参考资料

实例(3.8.8)

1
2
3
4
5
6
7
str = '+gaoyuanqi'

# 新字符串的长度为15
print(str.zfill(15))
# 宽度小于等于len(str)即10则返回原字符串
print(str.zfill(10))
print(str.zfill(10) == str)

zfill