str.rjust()
str.rjust(width[, fillchar]) str · Added in v3.x · Updated March 13, 2026 · String Methods The .rjust() method returns a right-justified version of the string padded with the specified fill character (default is space) to reach the given width. This is useful for formatting output in tables or console applications where you want numeric or right-aligned text to line up neatly.
Syntax
str.rjust(width, fillchar=' ')
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| width | int | Required | Minimum length of the returned string |
| fillchar | str | ’ ‘ | Character used for padding |
Returns: A new string of at least width length, right-justified.
Examples
Basic usage with spaces
>>> s = "hello"
>>> s.rjust(10)
' hello'
>>> len(s.rjust(10))
10
The string “hello” is 5 characters long. When you ask for a width of 10, Python adds 5 spaces to the left to reach the target length.
Custom fill character
>>> "Py".rjust(8, '*')
'******Py'
>>> "Code".rjust(10, '-')
'------Code'
You can use any character for padding, not just spaces. This is handy for creating visual separators or aligning numbers.
Width shorter than string
>>> "hello".rjust(3)
'hello'
If the specified width is less than or equal to the string’s length, the original string is returned unchanged.
Practical formatting example
>>> numbers = ["1", "25", "125", "1000"]
>>> for n in numbers:
... print(n.rjust(5) + " |")
1 |
25 |
125 |
1000 |
This creates neatly right-aligned numeric output where all numbers end at the same column position.
Aligning numeric tables
>>> data = [("Apples", 150), ("Oranges", 25), ("Bananas", 300)]
>>> for fruit, count in data:
... print(f"{fruit.rjust(10)} | {str(count).rjust(5)}")
Apples | 150
Oranges | 25
Bananas | 300
Right-justifying works well for numbers since it aligns digits by place value.
With f-strings (modern alternative)
>>> f"{'hello':>10}"
' hello'
Note that f-strings provide a more concise syntax for string alignment in Python 3.6+. The > specifier means right-align.
Common Patterns
Right-aligning prices
>>> prices = ["9.99", "100.00", "50.50"]
>>> max_len = max(len(p) for p in prices)
>>> for price in prices:
... print(price.rjust(max_len + 2))
9.99
100.00
50.50
Creating fixed-width columns for logs
>>> timestamps = ["10:00", "14:30", "09:15"]
>>> messages = ["Started", "Processing", "Complete"]
>>> for ts, msg in zip(timestamps, messages):
... print(f"[{ts.rjust(5)}] {msg}")
[10:00] Started
[14:30] Processing
[09:15] Complete
Padding version numbers
>>> versions = ["1.0", "1.10.2", "2.0.1"]
>>> for v in versions:
... print(f"v{v.rjust(10, '0')}")
v000001.0.0
v001.10.2
v0002.0.1
Errors
- If
fillcharis not exactly one character,TypeErroris raised - If
widthis not an integer,TypeErroris raised
>>> "hello".rjust(10, "ab")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: The fill character must be exactly one character long
>>> "hello".rjust("10")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer