python 中的ljust()
函數通過用給定的最小長度填充指定的字元來幫助將字元串向左對齊。在缺少 fillchar 參數的情況下,空格被視為默認填充字元。
**string.ljust(width[, fillchar])** #where width is an integer value
ljust()
函數接受兩個參數。fillchar 參數必須是單個字元。如果給定的 fillchar 參數有多個字元,那麼它將引發類型錯誤。
參數 | 描述 | 必需/可選 |
---|---|---|
寬度 | 返回字元串的寬度 | 需要 |
菲勒 | 填充缺少的空格的字元 | 可選擇的 |
返回值始終是字元串。這裡 fillchar 字元填充在字元串的右邊。如果指定的寬度小於或等於字元串的長度,則原始字元串作為輸出返回。
| 投入 | 返回值 |
| If 參數 | 左對齊字元串 |
# example string
string = 'Python'
length = 7
# print left justified string
print(string.ljust(length))
輸出:
Python
# example string
string = 'Python'
length = 10
fill = '*'
# print left justified string
print(string.ljust(length, fill))
輸出:
Python****
原創文章,作者:YSHLV,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127244.html