Python String rjust() Method

Instance

Returns a 20-character right-aligned version of the word "banana":

txt = "banana"
x = txt.rjust(20)
print(x, "is my favorite fruit.")

Run Instance

Note:As a result, there are actually 14 spaces on the left side of the word "banana".

Definition and Usage

The rjust() method uses the specified character (default is space) as the padding character to right-align the string.

Syntax

string.rjust(length, character)

Parameter Value

Parameter Description
length Required. The length of the returned string.
character Optional. Used to fill missing space characters (on the left side of the string). The default value is " " (space).

More Examples

Instance

Use the letter "O" as the padding character:

txt = "banana"
x = txt.rjust(20, "O")
print(x)

Run Instance