Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python String rjust() Method - WTMatter

Python String rjust() Method

Python String Rjust() Method

This is a tutorial of the Python String rjust() method. Learn to get a given string right-justified for a given minimum width with the help of examples.

Python String rjust()

The str.rjust() the method returns a right-justified string that is of given minimum width. The returned string is a new string created from the contents of the original string while the original string remains unchanged.


Given below is the syntax of the str.rjust() method.

rjust() Parameters

This method can take upto two different arguments that are described below.

  • width. It specifies the desired width of the right-justified string as an integer. It is mandatory to be provided.
  • fillchar. It is an optional argument and can be used to specify a character that you want this method to fill the remaining space with.

rjust() Return Value

It returns a right-justified string such that it is within the specified minimum width. In case, the fillchar argument is also supplied to the str.rjust() method, the remaining space of the string to be returned is filled with this character.


Given below are the two examples demonstrating the usage of the Python String rjust() method.

Example 1. right-Justify a String with a minimum width using rjust() method

In this example, we’re applying the method rjust() on two different string to justify them within the specified widths. Here we have also used the built-in len() method to check the length of the given string and the returned string.

#Sample String
line = "This string has to be justified to the right for the minimum width."

#String length of line

#Minimum Width
width = 100

rightJustifiedLine = line.rjust(width)
#Printing the right-justified string

#String Length of right-Justified String

As you can observe the output that the original string length is 67 and the length of the returned string is 100. It means that the rjust() method actually added 33 whitespaces before the start of the string to make the string length 100.


                                 This string has to be justified to the right for the minimum width.

Example 2. Using the Python String rjust() method with fillchar argument

In the following example, we’re making use of the rjust() method to right-justify a string but this time, we are passing both the arguments i.e. the width and the fillchar argument. In the two cases given in this example, we’ve used two different characters to fill the remaining space in the right-justified strings.

#Sample String
name = "Gurmeet Singh"

#String length of name

#Minimum Width
width = 25

#Filling Character
fillchar = "#" 

rightJustifiedName = name.rjust(width, fillchar)
#Printing the right-justified string

#String Length of right-Justified String


Here the original string length is 13 and the returned string has the string length of 25. It means that the rjust() method simply added 12 whitespaces before the start of the original string and filled it with the character provided in the fillchar argument i.e. #

############Gurmeet Singh

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *