Content · Programming · Python

Python str.ljust Method: Usage and Examples

str.ljust(width, fillchar) pads the right side of a string until the result reaches the requested character width. The default filler is spaces; it does not shorten an already longer string. Learn with a clear example.

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

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

Understanding the Concept


str.ljust(width, fillchar) pads the right side of a string until the result reaches the requested character width. The default filler is spaces; it does not shorten an already longer string.

When You Would Use This


Align product names in a simple plain-text invoice.

Step-by-Step Example


product_names = ["Pen", "Notebook"]
for product_name in product_names:
    print(product_name.ljust(12, ".") + " 1 item")

How the Example Works


Each product name is followed by enough dots to make a 12-character field. The original strings remain unchanged. Character width does not always equal visual column width for all Unicode text.

Example Output


Pen......... 1 item
Notebook.... 1 item

Try It Yourself


Create two rows with width 16 using spaces as the filler.

Common Beginner Mistakes


  • Assuming .ljust() pads on the left.
  • Using a multi-character fill string.
  • Assuming the method truncates strings that exceed the width.

Key Takeaways


  • str.ljust(width, fillchar) pads the right side of a string until the result reaches the requested character width.
  • Each product name is followed by enough dots to make a 12-character field.
  • Avoid this mistake: Assuming .ljust() pads on the left.
Share this resourceSend this page to someone who may find it useful.