Python 字符串 rjust() 方法
實例
返回單詞 "banana" 的 20 個字符長的右對齊版本:
txt = "banana" x = txt.rjust(20) print(x, "is my favorite fruit.")
注釋:結果是,"banana" 一詞的左側實際上有 14 個空格。
定義和用法
rjust() 方法將使用指定的字符(默認為空格)作為填充字符,將字符串右對齊。
語法
string.rjust(length, character)
參數值
參數 | 描述 |
---|---|
length | 必需。所返回字符串的長度。 |
character | 可選。用于填充缺失空間(在字符串的左側)字符。默認值為 " "(空格)。 |