Content · Programming · Python

Python str.rjust Method: Usage and Examples

str.rjust(width, fillchar) adds padding to the left side until the specified width is reached. This is useful for right-aligning numbers in plain-text reports. Learn with a clear example and common mistakes to avoid.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains str.rjust Method: Usage and Examples through a focused example and practical reasoning. No prior knowledge of this specific operation is required.

Understanding the Concept


str.rjust(width, fillchar) adds padding to the left side until the specified width is reached. This is useful for right-aligning numbers in plain-text reports.

When You Would Use This


Right-align quantities in a simple stock report.

Step-by-Step Example


quantities = [3, 45, 1200]
for quantity in quantities:
    print(str(quantity).rjust(6, " ") + " units")

How the Example Works


Each quantity occupies a six-character field aligned to the right. Converting the integer to a string is necessary before using a string method.

Example Output


3 units
    45 units
  1200 units

Try It Yourself


Display prices with a field width of 8 and two decimal places.

Common Beginner Mistakes


  • Calling .rjust() on a number without converting it to text.
  • Assuming it pads on the right rather than the left.
  • Expecting it to truncate content wider than the requested width.

Key Takeaways


  • str.rjust(width, fillchar) adds padding to the left side until the specified width is reached.
  • Each quantity occupies a six-character field aligned to the right.
  • Avoid this mistake: Calling .rjust() on a number without converting it to text.
Share this resourceSend this page to someone who may find it useful.