The rjust() function in python helps to align the string to right by filling specified characters with the given minimum length. Space is considered as the default fill character in case of missing fillchar parameter.
string.rjust(width[, fillchar]) #where width is an integer value
The rjust() function takes two parameters. The fillchar parameter must be a single character. If the given fillchar parameter has more than one character then it will throw a TypeError.
Parameter | Description | Required / Optional |
---|---|---|
width | width of the returned string | Required |
fillchar | A character to fill the missing space | Optional |
The return value is always a string. Here the fillchar character is filled in the left of the string. If the specified width is less than or equal to the length of the string, then the original string is returned as output.
Input | Return Value |
---|---|
If parameters | right-justified string |
# example string
string = 'Python'
length = 7
# print right justified string
print(string.rjust(length))
Output:
Python
# example string
string = 'Python'
length = 10
fill = '*'
# print left justified string
print(string.rjust(length, fill))
Output:
****Python