Python Reference Library →

Python String ljust() Method – Left Justify a String with Spaces or Characters

Let’s look at string ljust() method – left justify a string with spaces or characters

  1. Notice

    ljust() pads on the right so text is left-aligned in a minimum width. It returns a new string.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea'
result = text.ljust(8, ".")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · str'tea.....'
This step

ljust() pads on the right so text is left-aligned in a minimum width. It returns a new string.

Picture string ljust() method – left justify a string with spaces or characters

ljust() pads on the right so text is left-aligned in a minimum width. It returns a new string.

Image brief

Alt text
A diagram for Python String ljust() Method – Left Justify a String with Spaces or Characters. Instructions: text = 'tea' result = text.ljust(8, ".") print(result). Displayed result: tea...... The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea'; object 2: 'tea.....'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String ljust() Method – Left Justify a String with Spaces or Characters". Teaching focus: ljust() pads on the right so text is left-aligned in a minimum width. It returns a new string. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea' result = text.ljust(8, ".") print(result) | v DISPLAYED RESULT tea..... FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'tea' object 2: 'tea.....' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String ljust() Method – Left Justify a String with Spaces or Characters. Instructions: text = 'tea' result = text.ljust(8, ".") print(result). Displayed result: tea...... The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea'; object 2: 'tea.....'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea'
result = text.ljust(8, ".")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · str'tea.....'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
tea.....

What do you expect this program to print?

What does this example display?

text = 'tea'
result = text.ljust(8, ".")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

tea.....

Follow the printed values in order. The output is: tea.....

More examples and reference

Python String ljust() Method

The ljust() method in Python left-aligns a string by padding it with spaces (or a specified character) until it reaches the desired width.

Syntax

string.ljust(width, fillchar=' ')

Parameters:

  • width – Total length of the resulting string (including the original string and padding)
  • fillchar (optional) – The character to use for padding. Default is a space ' '

Returns:

  • A new string that is left-justified and padded to the specified width.

Example 1: Left-justify with spaces (default)

text = "Hello"
print(text.ljust(10))
Hello     

Explanation: The word "Hello" is padded with 5 spaces on the right to make the total length 10.

Example 2: Left-justify with a custom character

text = "Hi"
print(text.ljust(6, '-'))
Hi----

Explanation: The string "Hi" is padded with 4 hyphens '-' on the right.

Use Cases

  • Formatting tabular text output
  • Aligning labels or values in console applications
  • Creating fixed-width string formats

Common Mistakes

  • fillchar must be a single character. Using more than one character will raise an error.
  • If width is less than or equal to the string's length, the original string is returned unchanged.

Interview Tip

Use ljust() in string formatting questions where consistent alignment of text output is required.

Summary

  • ljust() pads the string on the right to make it left-aligned.
  • It returns a new string with the specified total width.
  • Great for creating aligned outputs and formatted strings.

Practice Problem

Write a Python program that prints a list of names left-aligned within 15 characters using ljust().

names = ["Alice", "Bob", "Charlie"]
for name in names:
    print(name.ljust(15) + "|")

Expected Output:

Alice          |
Bob            |
Charlie        |

What to remember

Connect what you noticed in the example with the idea below.

Remember

ljust() pads on the right so text is left-aligned in a minimum width. It returns a new string.

Watch out for this idea

ljust() pads on the left.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →