Python ljust() method returns a left-justified string and fills the remaining spaces with specified fillchar (default is a space). This method returns a new left-justified string and is filled with fillchars. The original string is returned if the width is less than len(s).
The syntax of the method is: str.ljust(width[, fillchar])
We’ll be covering the following topics in this tutorial:
String ljust() Parameters
ljust() method takes two parameters:
• width: width of the defined string. The original string is returned if the width is smaller than or equal to the string’s length.
• fillchar (Optional): the character for filling the remaining width space.
Return value from String ljust()
The ljust() function returns a new string and substitutes the given fill_char to the original string’s right.
Below is the python program to demonstrate the ljust() function:
str = "The Best Learning Resource For Online Education"; print (str.ljust(50, '#'))
When we run above the program, the outcome is as follows:
The Best Learning Resource For Online Education###
Left justify string of minimum width
# example string str = 'Online Education' width = 25 # print right justified string print(str.ljust(width))
When we run above the program, the outcome is as follows:
Online Education
Below are several other functions that we can use to work with string in Python 3