Python String rjust() Method



The Python String rjust() method, as the name suggests, will justify the string to a certain length to its right. Any character (letters, digits, or symbols) is added at the beginning of the string (performing Padding), based on the number of places the string shifts or the maximum length decided. However, if the string length after padding is less than the original string length, method does not make any changes to the string.

This method is a counterpart to the ljust() method; as ljust() justifies the string to its left.

Syntax

Following is the syntax for Python String rjust() method −

str.rjust(width[, fillchar])

Parameters

  • width − This is the string length in total after padding.

  • fillchar − This is the filler character; it is optional and its default is a space character.

Return Value

This method returns the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).

Example

When we pass the total string length and digits to the fillchar parameter, the method returns the justified string to its right

The following example shows the usage of Python String rjust() method. Here, we are creating a string "this is string example....wow!!!" and calling the method rjust() by passing 50 and '0' as width and fillchar parameters.

 
str = "this is string example....wow!!!";
print(str.rjust(50, '0'))

When we run above program, it produces following result −

000000000000000000this is string example....wow!!!

Example

When we pass the total string length as width and letters as the fillchar parameter, the method returns the justified string to its right

 
str = "Tutorialspoint";
print(str.rjust(14, '#'))

The output for the given program is displayed as follows −

Tutorialspoint

Example

If we try to pass more than one character as the fillchar parameter, this method generates an error.

 
str = "Tutorials";
print(str.rjust(20, '#$$'))

The output for the given program is displayed as follows −

Traceback (most recent call last):
  File "main.py", line 2, in 
    print(str.rjust(20, '#$$'))
TypeError: The fill character must be exactly one character long

Example

When we pass only the width as the parameter, the method returns the justified string to its right using the default fillchar (space)

In the given example program, we are creating a string input and passing the total string length as a parameter to the method. As the optional parameter is not specified, the default value space is taken.

 
str = "Tutorials";
print(str.rjust(25))

The output for the given program is displayed as follows −

                Tutorials

Example

When the width parameter passed is less than the length of the string, the method returns the original string

In the given example program, we are creating a string by passing the width and fillchar parameters to the method. However, the width parameter passed is less than the original length of the string. The return value will be obtained as the original string.

 
str = "Tutorials";
print(str.rjust(5, '$'))

The output for the given program is displayed as follows −

Tutorials
python_strings.htm
Advertisements