.rjust()
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.
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 tolength
minus the length ofstring_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-----------endurantismexdurantism
Codebyte Example
The code below is runnable, change the value of your_string
, your_length
, and your_character
to test the .rjust()
method.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours