Python .rjust()

noahpgordon's avatar
Published Oct 26, 2023
Contribute to Docs

The method .rjust() adds padding of a specified length to the left of a given string. The user can specify a character to use as the padding. The number of padding characters added is equal to the length specified in the method call minus the length of the string provided.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

parameter_string.rjust(length, padding_character)

Required Parameters

  • parameter_string: The string to be modified with padding.
  • length: An integer used to determine how many characters of padding will be added. The number of padding characters will be equal to length minus the length of string_parameter.

Optional Parameters

  • padding_character: The character used as the padding. Must be a single character. Default value is a space (" ").

Example

The following are example calls to .rjust() using different parameters:

print("perdurantism".rjust(17))
print("endurantism".rjust(22, "-"))
print("exdurantism".rjust(5, "x"))

The code above gives the following output:

perdurantism
-----------endurantism
exdurantism

Codebyte Example

The code below is runnable, change the value of your_string, your_length, and your_character to test the .rjust() method.

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours