Python - String Method - rjust() Tutorial
This method returns a right-justified(right-aligned) string of given minimum width, using a specified character (space as default) as the fill character.
Syntax-
String.rjust(length, fillchar)
#length - It is the total length of the string that needs to be returned
after filling the specified characters on the left end(beginning) of the actual string.
#fillchar - (Optional) It is the specified character that needs to be filled
on the left end(beginning) of the actual string. By default it is space
Example-
str1 = "Hello Python"
print(str1.rjust(30))
print(str1.rjust(30,'*'))
print(str1.rjust(2))
Output-
Hello Python
******************Hello Python
Hello Python
In the above program,
Line 2- str1 is aligned at right and remaining is aligned with spaces on the left, with the total length of the new string as 30
Line 3- str1 is aligned at right and remaining is aligned with specified character "*" on the left, with the total length of the new string as 30
Line 4- On providing length less than the original string. then it will return the original string.