Python字串zfill()方法

Python字串zfill()方法使用零填充字串,使用零填充寬度。

語法

以下是zfill()方法的語法 -

str.zfill(width)

參數

  • width - 這是字串的最終寬度,這是填充零後會得到的寬度。

返回值

  • 此方法返回填充字串。

示例

以下示例顯示了zfill()方法的用法 -

#!/usr/bin/python3

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

當運行上面的程式,它產生以下結果 -

str.zfill :  00000000this is string example....wow!!!
str.zfill :  000000000000000000this is string example....wow!!!

上一篇: Python字串 下一篇: Python列表