Tutorial Study Image

Python rjust()

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
 

rjust() Parameters:

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

rjust() Return Value

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

Examples of rjust() method in Python

Example 1: Ho to right justify string of minimum width in Python?


# example string
string = 'Python'
length = 7

# print right justified string
print(string.rjust(length))
 

Output:


    Python

Example 2: How to right justify string and fill the remaining spaces in Python?


# example string
string = 'Python'
length = 10
fill = '*'

# print left justified string
print(string.rjust(length, fill))
 

Output:


****Python