The rjust()
method in Python is a String Method used to right-justify a string by padding it with spaces on the left side. It takes two parameters: the desired length of the justified string and the character to use for padding (default is space).
Parameter Values
Parameter | Description |
---|---|
width | The specified width of the resulting string, where the original string is right-justified in. If the width is less than or equal to the length of the original string, the original string is returned as is. |
fillchar | The character used for filling the remaining space when justifying the original string to the right. It defaults to space (' '). |
Return Values
The rjust()
method in Python returns a str
with the original string right-justified.
How to Use rjust()
in Python
The rjust()
method will right align a string by padding it with spaces on the left to a specific width. If the string is already as wide or wider than the specified width, it will return the original string.
'Hello'.rjust(10)
The rjust()
method can also be used with a custom fill character instead of spaces. For example, padding a string with '-' on the left to a width of 15.
'World'.rjust(15, '-')
The rjust()
method is helpful for formatting text-based tables or blocks of text, ensuring consistent alignment.
'Python'.rjust(20, '*')