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