Python zfill()方法

描述

Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。

语法

zfill()方法语法:

str.zfill(width)

参数

width – 指定字符串的长度。原字符串右对齐,前面填充0。
这个函数在大数加法算法中十分常用。

示例代码

s="1001"
t="1002000000000000000000"
maxlen = max(len(s), len(t))
print(s)
print(t)
s = s.zfill(maxlen)
t = t.zfill(maxlen)
print(s)
print(t)

输出

1001
1002000000000000000000
0000000000000000001001
1002000000000000000000

猜你喜欢

转载自blog.csdn.net/mao_hui_fei/article/details/114292333