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.
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
rightJustifiedString = givenString.rjust(width, fillchar)
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
Example 1. right-Justify a String with a minimum width using
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 print(len(line)) #Minimum Width width = 100 rightJustifiedLine = line.rjust(width) #Printing the right-justified string print(rightJustifiedLine) #String Length of right-Justified String print(len(rightJustifiedLine))
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.
67 This string has to be justified to the right for the minimum width. 100
Example 2. Using the Python String
rjust() method with
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 print(len(name)) #Minimum Width width = 25 #Filling Character fillchar = "#" rightJustifiedName = name.rjust(width, fillchar) #Printing the right-justified string print(rightJustifiedName) #String Length of right-Justified String print(len(rightJustifiedName))
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. #
13 ############Gurmeet Singh 25
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!