python字符串操作--zfill()方法用0填充前缀 发表于 2020-12-27 分类于 Python 返回长度为 width 的新字符串,原字符串右居,使用ASCII 0 数码填充左边的空位。如果有正负值(+-)前缀,则 0 填充在正负值之后 格式及参数1str.zfill(width) str : 字符串对象 width : 新字符串的总宽度,小于等于 len(str) 时则返回原字符串 参考资料 实例(3.8.8)1234567str = '+gaoyuanqi'# 新字符串的长度为15print(str.zfill(15))# 宽度小于等于len(str)即10则返回原字符串print(str.zfill(10))print(str.zfill(10) == str)