Python字串ljust()
方法返回長度為width
的左對齊的字串。使用指定的fillchar
(默認為空格)填充完成。 如果寬度小於len(s)
,則返回原始字串。
語法
以下是ljust()
方法的語法 -
str.ljust(width[, fillchar])
參數
- width - 這是填充後總共的字串長度。
- fillchar - 這是用於填充字元,默認是一個空格。
返回值
- 此方法返回長度為
width
的字串左對齊的字串。使用指定的fillchar
(默認為空格)字元完成填充。 如果寬度小於len(s)
,則返回原始字串。
示例
以下示例顯示了ljust()
方法的用法。
#!/usr/bin/python3
str = "this is string example....wow!!!"
print str.ljust(50, '*')
當運行上面的程式,它產生以下結果 -
this is string example....wow!!!******************