str.rjust(width, fillchar=' ') -> str
回傳長度至少為 width 的新字串,原字串靠右,左側以 fillchar 填充。若 len(s) >= width,直接回傳原字串副本。
>> 'hello'.rjust(10)
' hello'
>>> 'hello'.rjust(10, '-')
'-----hello'
>>> 'hello'.rjust(3)
'hello'
>>> ''.rjust(5, '*')
'*****'
輸出:
Revenue 1,234,567 Expense 234,567 Profit 1,000,000
| 方法 | 對齊 | 範例 (width=10, fill='-') |
|---|---|---|
ljust | 靠左 | 'hello-----' |
rjust | 靠右 | '-----hello' |
center | 置中 | '--hello---' |
>> f'{"hello":>10}'
' hello'
>>> f'{"hello":->10}'
'-----hello'
>>> f'{1234:>10,}'
' 1,234'
>>> f'{1234:0>10,}'
'00001,234'
格式化字串支援數字格式化 (千分位、小數位、正負號)、對齊、填充字一次完成,語意更清晰。
fillchar 必須長度 1。wcwidth 計算視覺寬度後再填充。