VOOZH about

URL: https://www.geeksforgeeks.org/python/python-string-ljust-method/

⇱ Python String ljust() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python String ljust() Method

Last Updated : 23 Jul, 2025

Python ljust() method is used to left-justify a string, padding it with a specified character (space by default) to reach a desired width. This method is particularly useful when we want to align text in a consistent format, such as in tables or formatted outputs.

Here's a basic example of how to use ljust() method:


Output
Hello 

Explanation:

  • In this example, the string "Hello" is left-justified to a width of 10 characters, with spaces added to the right.

Syntax of ljust() method

string.ljust(width, fillchar=' ')

Parameters:

  • width: The total width of the resulting string. If the original string is shorter than this width, it will be padded with the fillchar.
  • fillchar (optional): The character to use for padding. The default is a space.

Return Type:

  • Returns a new string of given length after substituting a given character in right side of original string.

Using the default fill character

The ljust() method can be used without specifying a fillchar, in which case it defaults to a space.


Output
Python 

Explanation:

  • The string s is left-aligned in a field of width 12.
  • The remaining space is filled with spaces.

Specifying a custom fill character

Sometimes, we may want to use a character other than a space to fill the remaining space. The ljust() method allows you to specify a custom fillchar.


Output
Data------

Explanation:

  • The fillchar parameter is set to '-', so the remaining space is filled with dashes.
  • This can be useful for creating custom formatting styles.

Combining ljust() with other string methods

ljust() method can also be used alongside other string methods to achieve more complex formatting.


Output
ALIGN 

Explanation:

  • The string s is left-aligned in a field of width 10.
  • The resulting string is then converted to uppercase using the .upper() method.
Comment