1. 程式人生 > >Python 中的 zfill()(關鍵詞:Python/zfill)

Python 中的 zfill()(關鍵詞:Python/zfill)

描述

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

語法

zfill()方法語法:

str.zfill(width)

引數

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

返回值

返回指定長度的字串。

例項

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

>>> string = 'hello'
>>> string.zfill(10)
'00000hello'
>>> string.zfill(8)
'000hello'

參考文獻:
1. http://www.runoob.com/python/att-string-zfill.html