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/n/128662.html
 
 微信扫一扫
微信扫一扫  支付宝扫一扫
支付宝扫一扫 
