Python字符串 rjust() 方法
Python String rjust() 方法在替换原始字符串左侧的给定字符后返回给定长度的新字符串。
Syntax:
string.rjust(length, fillchar)
Parameters:
- length: length of the modified string. If length is less than or equal to the length of the original string then original string is returned.
- fillchar: (optional) characters which needs to be padded. If it’s not provided, space is taken as a default argument.
Return Value:
Returns a new string of given length after substituting a given character in left side of original string.
示例 1
Python3
# Python program to demonstrate working of
# rjust()
string = 'geeks'
length = 8
# If no fill character is provided, space
# is used as fill character
print(string.rjust(length))
Python3
# example string
string = 'geeks'
length = 8
fillchar = '*'
print(string.rjust(length, fillchar))
输出:
geeks
示例 2
蟒蛇3
# example string
string = 'geeks'
length = 8
fillchar = '*'
print(string.rjust(length, fillchar))
输出:
***geeks