Python zfill() 方法

时间:2023-03-09 03:37:37
Python zfill() 方法

描述

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

语法

zfill()方法语法:

S.zfill(width)

参数

  • width -- 指定字符串的长度。原字符串右对齐,前面填充0。

返回值

返回指定长度的字符串。

实例

以下实例展示了 zfill() 方法的使用方法:

#!/usr/bin/python3

S = "this is string example from runoob....wow!!!"
print ("S.zfill : ",S.zfill(40))
print ("S.zfill : ",S.zfill(50))

以上实例输出结果如下:

S.zfill :  this is string example from runoob....wow!!!
S.zfill : 000000this is string example from runoob....wow!!!