Python ljust()方法

Python 字串 Python 字串


描述

Python ljust() 方法返回一個原字串左對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字串。

語法

ljust()方法語法:

str.ljust(width[, fillchar])

參數

  • width -- 指定字串長度。
  • fillchar -- 填充字元,默認為空格。

返回值

返回一個原字串左對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字串。

實例

以下實例展示了ljust()的使用方法:

#!/usr/bin/python

str = "this is string example....wow!!!";

print str.ljust(50, '0');

以上實例輸出結果如下:

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

Python 字串 Python 字串