Latest web development tutorials

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字符串