How does rjust() work in Python?

The rjust() method in Python is used to return a right-justified version of the string that is calling the method.

Syntax

str.rjust(length, char)

Parameters

This method requires the length of the returned string to be passed as a parameter.

char is an optional parameter used to fill the remaining space of width in the string.

Code

Example 1

string = "This is a string."
print(string.rjust(30))

Example 2

string = "This is a string."
print(string.rjust(30, '0'))