The output textual content is aligned in programming to make the textual content readable or to make columns of tabular knowledge or stories aligned correctly. Textual content alignment may be carried out to manage spacing and padding between completely different characters. In Python, the “ljust()” methodology is used to retrieve a brand new string that’s left-justified with a specified character (default is house) and refill the remaining house on the best.
This write-up will ship an entire tutorial on the Python “string.ljust()” methodology utilizing quite a few examples through the under content material:
What’s the “string.ljust()” Methodology in Python?
In Python, the “string.ljust()” methodology is used to left-justify the enter string and fill the remaining areas by including a specified character (default is about to “empty house”).
Syntax
string.ljust(size, character)
Parameter Values
Within the above syntax:
- The “size” parameter is utilized to point the size of the retrieved string.
- The “character” parameter is used to specify the character that fills the lacking house. If the worth isn’t entered, the default house “” is inserted.
Return Worth
The “string.ljust()” methodology retrieves the left-justified string.
Instance 1: Left Justify the String With out Filling the Remaining Areas
The next code is used to left-justify the string with out filling the remaining house:
string_value = “Python Information”
print(string_value.ljust(20))
Within the above code, the “string.ljust()” methodology takes the returned string size “20” as an argument and justifies the enter string to the left.
Output
The enter string has been left justified.
Instance 2: Left Justify the Python String and Fill/Add Worth to Empty/Remaining Areas
The under instance code left justifies the required string and fills the empty/remaining areas:
string_value = “Python Information”
print(string_value.ljust(20, ‘-‘))
On this code snippet, the “string.ljust()” methodology takes the returned string size “20” and the filling character “–” as its arguments, respectively, to left-justify the initialized string and fill the remaining areas.
Output
The outlined string has been left justified, and the empty areas are occupied with the “–” character accordingly.
Instance 3: Including Areas Between the Listing Objects/Components
The below-stated code is used to insert house between the actual checklist objects:
list1 = [‘ALex’, ‘Anna’, ‘Joseph’]
for i in list1:
print(str(i).ljust(10, ‘ ‘), finish=“”)
In keeping with the above code:
- The “for” loop iterates over the outlined checklist.
- The “str()” operate converts the checklist objects into strings.
- The “ljust()” methodology takes the returned string size “10” as an argument and left-justifies all of the checklist parts by retaining the house between the checklist objects.
Output
The enter checklist parts have been left justified with a clean house appropriately.
Conclusion
In Python, the “string.ljust()” methodology is utilized to left justified/align the required string with the given returned string size worth. Nonetheless, the strings whose width or returned string size exceeds their size are stuffed with “fillchar”. This write-up offered an in-depth tutorial on Python’s “string.ljust()” methodology with acceptable examples.