Python3 string.zfill()方法

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!!!

上一篇: Python3數字 下一篇: Python3字串