Python ljust() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

ljust() makes a string at least width characters long by adding padding on the right, so the text stays at the left. "hi".ljust(5) is "hi ", which is the text plus three spaces.

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: ljust() never cuts text. The method returns a new string.

text.ljust() 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.ljust(width)
padded = text.ljust(width, fillchar)
CallWhat it does
"hi".ljust(5)Returns 'hi '. Three spaces are added on the right.
"hi".ljust(5, ".")Returns 'hi...'. The padding is made of dots.
"python".ljust(3)Returns 'python'. The text is already longer than the width, so it is unchanged.

To pad on the left use rjust(), to centre the text use center(), and to pad a number with zeros use zfill().

Library declarations
python
str.ljust(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 left. The original string is not changed.

Python Examples

Example 1 — Left-Align Text in a Field

Pad a name to 8 characters, with spaces and with dots, and try a width that is too small.

python
name = "Ada"
padded = name.ljust(8)

print(repr(padded))
print(len(padded))
print(name.ljust(8, "."))
print(name.ljust(2))

How It Works

  1. Spaces on the right. The name has 3 characters, so 5 spaces are added to reach 8. repr() makes the spaces visible.
  2. The length is the width. len(padded) is 8.
  3. A fill character. The second argument replaces the spaces with dots.
  4. A small width does nothing. The width 2 is smaller than the text, so Ada is returned unchanged.

Example 2 — Print a Simple Price List

Pad each name to the same width so the prices start in the same column.

python
prices = [("tea", 3.5), ("coffee", 4.25), ("milk", 2)]

for name, price in prices:
    print(name.ljust(8) + str(price))

How It Works

  1. A list of pairs. The loop unpacks each pair into name and price.
  2. The same width every time. name.ljust(8) makes every name 8 characters long, whatever its own length.
  3. The prices line up. Every price starts in column 9, because every name fills 8 columns.

Example 3 — Make a Table of Contents

Use dots as the fill character to connect titles with page numbers.

python
chapters = [("Intro", 1), ("Basics", 14), ("Loops", 27)]

for title, page in chapters:
    print(title.ljust(12, ".") + str(page))

How It Works

  1. Dots instead of spaces. The second argument "." is used for the padding.
  2. Different dot counts. Intro gets 7 dots, and Basics gets 6, so each title fills 12 characters.
  3. Page numbers line up. Every number begins right after column 12.

Frequently Asked Questions

It returns the string padded on the right to a given width, so the text is aligned to the left. The padding is spaces by default.
ljust() adds the padding on the right, so the text is at the left. rjust() adds the padding on the left, so the text is at the right.
No. The fill character must be exactly one character, otherwise Python raises TypeError.
Nothing is changed. The method never cuts the text, so the original text is returned.
No. Strings are immutable, so ljust() returns a new string. Assign the result to keep it.

Next: lower()

Return a copy of the string with every letter in lower case.

lower() 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.

13 people found this page helpful