Python String ljust() Method
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 “ljust()” Method in Python?
- Left Justify the String Without Filling the Remaining Spaces.
- Left Justify the Python String and Fill/Add Value to Empty/Remaining Spaces.
- Adding Spaces Between the List Items/Elements.
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”).
Syntax
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:
print(string_value.ljust(20))
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.
Output
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:
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.
Output
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:
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.
Output
The input list elements have been left justified with a blank space appropriately.
Conclusion
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.
Source: linuxhint.com