Python String rjust() Method Tutorial

In this section, we will learn what String rjust() method is and how to use it in Python.

What is String rjust() Method in Python?

The Python string rjust() method is used to align the left side of a string value.

Basically, using this method we can increase the length of a string value and add new white spaces on the left side of that string so that it becomes align with other content.

Python String rjust() Method Syntax:

string.rjust(length, character)

String rjust() Method Parameter

The method takes two arguments:

  • The first argument is an integer number that defines a length. This value declares the final length of the new string that will return from this method. Also, this value declares how far the left side of the target string should be indented! For example, if the string value that this method is invoked upon has the length of 5, then setting the first argument to a value like 15, will cause to add 10 white spaces to the left side of that string.
  • The second argument is a value (usually just a character) that we want to fill spaces with the new string. By default, this value is set to a white-space and that means the length that left out should be filled with white spaces! But we can use other characters if we want for this purpose.

String rjust() Method Return Value

The return value of this method a new string with its left side being indented according to the values we put as the argument of the method.

Example: using python string rjust() method

s1 = "My name is John Doe"

print(s1.rjust(50, "*"))

Output:

*******************************My name is John Doe
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies