Python rjust() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

rjust() makes a string at least width characters long by adding padding on the left, so the text stays at the right. "7".rjust(3) is " 7", which is two spaces plus the text.

The padding is made of spaces unless you pass a one-character fillchar. If the string is already as long as width, or longer, it is returned as it is: rjust() never cuts text. The method returns a new string.

text.rjust() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Pass the total width. The fill character is optional.

python
padded = text.rjust(width)
padded = text.rjust(width, fillchar)
CallWhat it does
"7".rjust(3)Returns ' 7'. Two spaces are added on the left.
"7".rjust(3, "0")Returns '007'. The padding is made of zeros.
"-7".rjust(4, "0")Returns '00-7'. The sign is treated as an ordinary character, which is why zfill() is better for numbers.

To pad on the right use ljust(), to centre the text use center(), and to pad a number with zeros and keep its sign use zfill().

Library declarations
python
str.rjust(width[, fillchar]) -> str
# returns: a new string at least width characters long
# raises: TypeError if fillchar is not exactly one character

Parameters

ParameterDescription
widthRequired. The length of the final string. If it is not larger than the length of the text, nothing is added.
fillcharOptional. The character used for padding. It must be a string of exactly one character. Default is a space.

Return Value

Return ValueDescription
strA new string of length max(width, len(text)), with the text at the right. The original string is not changed.

Python Examples

Example 1 — Right-Align Text in a Field

Pad a number to 6 characters with spaces and with stars, and try a width that is too small.

python
number = "42"

print(repr(number.rjust(6)))
print(number.rjust(6, "*"))
print(number.rjust(1))
print(len(number.rjust(6)))

How It Works

  1. Spaces on the left. The text has 2 characters, so 4 spaces are added before it to reach 6.
  2. A fill character. The second argument replaces the spaces with stars.
  3. A small width does nothing. The width 1 is smaller than the text, so 42 is returned unchanged.
  4. The length is the width. The padded text has 6 characters.

Example 2 — Print a Column of Numbers

Right-align numbers so that the digits line up under each other.

python
totals = [5, 120, 3400]

for value in totals:
    print(str(value).rjust(6))

How It Works

  1. Convert first. str(value) turns each number into text, because rjust() is a string method.
  2. The same width. Every line is 6 characters long.
  3. The digits line up. The last digit of every number is in the same column, which is how people expect numbers to look.

Example 3 — The Minus Sign Trap

Compare rjust() with zfill() when the number is negative.

python
print("7".rjust(3, "0"))
print("-7".rjust(4, "0"))
print("-7".zfill(4))

How It Works

  1. A positive number works. Two zeros are added on the left, giving 007.
  2. A negative number breaks. rjust() does not know about signs, so the zeros go before the minus sign, giving 00-7.
  3. zfill() knows about signs. It keeps the minus sign at the front and pads after it, giving -007.

Frequently Asked Questions

It returns the string padded on the left to a given width, so the text is aligned to the right. The padding is spaces by default.
Convert it to a string and use rjust(width, "0"), or use zfill(width), which also handles a leading sign correctly.
rjust() adds the padding on the left, so the text is at the right. ljust() adds the padding on the right, so the text is at the left.
Nothing is changed. The method never cuts the text, so the original text is returned.
No. Strings are immutable, so rjust() returns a new string. Assign the result to keep it.

Next: rpartition()

Split a string at the last separator into a tuple of three parts: before, separator, after.

rpartition() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful