Jump to content

Python String ljust() Method

Linux Hint

Recommended Posts

The output text is aligned in programming to make the text readable or to make columns of tabular data or reports aligned properly. Text alignment can be performed to control spacing and padding between different characters. In Python, the “ljust()” method is used to retrieve a new string that is left-justified with a specified character (default is space) and fill up the remaining space on the right.

This write-up will deliver a complete tutorial on the Python “string.ljust()” method using numerous examples via the below content:

What is the “string.ljust()” Method in Python?

In Python, the “string.ljust()” method is used to left-justify the input string and fill the remaining spaces by adding a specified character (default is set to “empty space”).


string.ljust(length, character)


Parameter Values

In the above syntax:

  • The “length” parameter is utilized to indicate the length of the retrieved string.
  • The “character” parameter is used to specify the character that fills the missing space. If the value is not entered, the default space “” is inserted.

Return Value

The “string.ljust()” method retrieves the left-justified string.

Example 1: Left Justify the String Without Filling the Remaining Spaces

The following code is used to left-justify the string without filling the remaining space:

string_value = "Python Guide"


In the above code, the “string.ljust()” method takes the returned string length “20” as an argument and justifies the input string to the left.



The input string has been left justified.

Example 2: Left Justify the Python String and Fill/Add Value to Empty/Remaining Spaces

The below example code left justifies the specified string and fills the empty/remaining spaces:

string_value = "Python Guide"
print(string_value.ljust(20, '-'))


In this code snippet, the “string.ljust()” method takes the returned string length “20” and the filling character “” as its arguments, respectively, to left-justify the initialized string and fill the remaining spaces.



The defined string has been left justified, and the empty spaces are occupied with the “” character accordingly.

Example 3: Adding Spaces Between the List Items/Elements

The below-stated code is used to insert space between the particular list items:

list1 = ['ALex', 'Anna', 'Joseph']
for i in list1:
    print(str(i).ljust(10, ' '), end="")


According to the above code:

  • The “for” loop iterates over the defined list.
  • The “str()” function converts the list items into strings.
  • The “ljust()” method takes the returned string length “10” as an argument and left-justifies all the list elements by keeping the space between the list items.



The input list elements have been left justified with a blank space appropriately.


In Python, the “string.ljust()” method is utilized to left justified/align the specified string with the given returned string length value. However, the strings whose width or returned string length exceeds their length are filled with “fillchar”. This write-up provided an in-depth tutorial on Python’s “string.ljust()” method with appropriate examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...