Python的zfill( )函數

  • 2019 年 10 月 7 日
  • 筆記

原文鏈接:https://www.runoob.com/python/att-string-zfill.html

描述

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

語法

zfill()方法語法:

str.zfill(width)

參數

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

返回值

返回指定長度的字元串。

實例

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

#!/usr/bin/python    str = "this is string example....wow!!!";    print str.zfill(40);  print str.zfill(50);

以上實例輸出結果如下:

00000000this is string example....wow!!!  000000000000000000this is string example....wow!!!

Tags: