Latest web development tutorials

Python3 zfill()方法

Python3 字符串 Python3字符串


描述

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

語法

zfill()方法語法:

str.zfill(width)

參數

  • width -- 指定字符串的長度。 原字符串右對齊,前面填充0。

返回值

返回指定長度的字符串。

實例

以下實例展示了zfill()函數的使用方法:

#!/usr/bin/python3

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

以上實例輸出結果如下:

str.zfill :  this is string example from w3big....wow!!!
str.zfill :  000000this is string example from w3big....wow!!!

Python3 字符串 Python3字符串