Python 的 str.rjust(~) 方法返回具有指定長度的右對齊(對齊)字符串。
參數
1. width | number
返回字符串的長度。
2. fillchar | character | optional
用於填充的字符。默認為" "(空格)。
返回值
右對齊的字符串填充到指定的 width 。
如果指定的width小於原始字符串的長度,則返回原始字符串。
例子
基本用法
要為 "Dog" 返回長度為 6 的右對齊字符串:
x = "Dog"
x.rjust(6)
' Dog'
請注意,Dog 之前有 3 個空格字符,以使返回的字符串為 6 字符。
填充字符參數
要使用 '*' 作為填充字符,為 "Dog" 返回長度為 6 的右對齊字符串:
y = "Dog"
y.rjust(6, '*')
'***Dog'
現在'Dog' 之前有三個'*' 字符,使得返回的字符串為6 字符。
寬度小於原始字符串的長度
要使用 '*' 作為填充字符,為 "Dog" 返回長度為 2 的右對齊字符串:
z = "Dog"
z.rjust(2, '*')
'Dog'
由於 2 提供的寬度小於 "Dog" 的長度,因此返回原始字符串的副本。
相關用法
- Python String rjust()用法及代碼示例
- Python String rstrip方法用法及代碼示例
- Python String rsplit()用法及代碼示例
- Python String rpartition()用法及代碼示例
- Python String rpartition方法用法及代碼示例
- Python String replace()用法及代碼示例
- Python String rindex()用法及代碼示例
- Python String rindex方法用法及代碼示例
- Python String rstrip()用法及代碼示例
- Python String rfind方法用法及代碼示例
- Python String rsplit方法用法及代碼示例
- Python String replace方法用法及代碼示例
- Python String rfind()用法及代碼示例
- Python String count方法用法及代碼示例
- Python String isnumeric方法用法及代碼示例
- Python String Center()用法及代碼示例
- Python String zfill方法用法及代碼示例
- Python String decode()用法及代碼示例
- Python String count()用法及代碼示例
- Python String join()用法及代碼示例
- Python String casefold()用法及代碼示例
- Python String isalnum()用法及代碼示例
- Python String endswith方法用法及代碼示例
- Python String isidentifier()用法及代碼示例
- Python String startswith()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Python String | rjust method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
