Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python String ljust() Method - WTMatter

Python String ljust() Method

Python String Ljust() Method

This is a tutorial of the Python String ljust() method. Learn to get a given string left-justified for a given minimum width with the help of examples.

Python String ljust()

The str.ljust() the method returns a left-justified string that is of given minimum width. The returned string is a new string created from the contents of the original string while the original string remains unchanged.


Given below is the syntax of the str.ljust() method.

ljust() Parameters

This method can take upto two different arguments that are described below.

  • width. It specifies the desired width of the left-justified string as an integer. It is mandatory to be provided.
  • fillchar. It is an optional argument and can be used to specify a character that you want this method to fill the remaining space with.

ljust() Return Value

It returns a left-justified string such that it is within the specified minimum width. In case, the fillchar argument is also supplied to the str.ljust() method, the remaining space of the string to be returned is filled with this character.


Given below are the two examples demonstrating the usage of the Python String ljust() method.

Example 1. Left-Justify a String with a minimum width using ljust() method

In this example, we’re applying the method ljust() on two different string to justify them within the specified widths. Here we have also used the built-in len() method to check the length of the given string and the returned string.

#Sample String
line = "This string has to be justified to the left for the minimum width."

#String length of line

#Minimum Width
width = 100

leftJustifiedLine = line.ljust(width)
#Printing the left-justified string

#String Length of Left-Justified String

As you can observe the output that the original string length is 66 and the length of the returned string is 100. It means that the ljust() method actually added 34 whitespaces after the end of the string to make the string length 100.


This string has to be justified to the left for the minimum width.                                  

Example 2. Using the Python String ljust() method with fillchar argument

In the following example, we’re making use of the ljust() method to left-justify a string but this time, we are passing both the arguments i.e. the width and the fillchar argument. In the two cases given in this example, we’ve used two different characters to fill the remaining space in the left-justified strings.

#Sample String
name = "Gurmeet Singh"

#String length of name

#Minimum Width
width = 25

#Filling Character
fillchar = "#" 

leftJustifiedName = name.ljust(width, fillchar)
#Printing the left-justified string

#String Length of Left-Justified String


Here the original string length is 13 and the returned string has the string length of 25. It means that the ljust() method simply added 12 whitespaces after the end of the original string and filled it with the character provided in the fillchar argument i.e. #

Gurmeet Singh############

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *