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.
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
leftJustifiedString = givenString.ljust(width, fillchar)
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
Example 1. Left-Justify a String with a minimum width using
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 print(len(line)) #Minimum Width width = 100 leftJustifiedLine = line.ljust(width) #Printing the left-justified string print(leftJustifiedLine) #String Length of Left-Justified String print(len(leftJustifiedLine))
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.
66 This string has to be justified to the left for the minimum width. 100
Example 2. Using the Python String
ljust() method with
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 print(len(name)) #Minimum Width width = 25 #Filling Character fillchar = "#" leftJustifiedName = name.ljust(width, fillchar) #Printing the left-justified string print(leftJustifiedName) #String Length of Left-Justified String print(len(leftJustifiedName))
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. #
13 Gurmeet Singh############ 25
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!