ljust() in Python
Overview
Python offers many functions to align the string or add user-specified padding instead of blank space in the string. These functions are ljust, rjust, and center, which are used to align the string differently.
Syntax for ljust() in Python
Parameters for ljust() in Python
- width: (Required), the width of the string till it is expanded. The original string will be returned if it is less than or equal to the specified string's length.
- fillchar: (Optional), A character to fill the remaining space in the left-justified string if the width is greater than its length. If the character is not specified, a blank space will be added right to the string.
Return Value for ljust() in Python
It returns a new string of a specific length after inserting a specified character, i.e., fillchar on the right side of the old string. If fillchar is specified, the remaining space is filled with the specified character.
Example for ljust() in Python
Output:
In the above example, you can see that text.ljust(8)
creates a new string of length 10 and makes the given string left-justified. Since we have not defined the fillchar, the default value space is added.
Similarly, text.ljust (8, "-")
gives us output as Scaler--.
Here ljust function appends two - as a fillchar to make the string of the given length. If the given width is the same or less than the length of the original string, the function will do nothing. It will just return the original string, as you can see above in the third example.
What is ljust() in Python?
ljust in python is a function that returns a left-justified string with a length equal to the specified width. It fills the remaining string space with blank spaces if the fillchar argument is not passed.
How to Use ljust() in Python?
ljust is a method in Python that is used to the left specified the given string. It takes two parameters as an argument, i.e., width and fillchar.
The width specifies the maximum length to which the given string will be expanded, where fillchar specifies which character should be used to fill in the blank spaces in the string after it has been left-justified.
It returns a new string of a specific length after inserting a specified character, i.e., fillchar on the right side of the old string.
Note: The fillchar character must be a single character. If we give fillchar more than one character, it will throw a TypeError.
More Examples
Example 1
Output:
In the above example, you can see that text.ljust(10)
creates a new string of length 10 and makes the given string left-justified. Since we have not defined the fillchar, the default value space is added.
Similarly, text.ljust (10, "$")
gives us output as Scaler$$.
Here ljust() function appends two $ as a fillchar to make the string of the given length. If the given width is the same or less than the length of the original string, the function will do nothing. It will just return the original string, as you can see above in the third example.
Example 2
Output:
The fillchar character must be a single character. If we give fillchar more than one character, it will throw a TypeError, as shown above.
Conclusion
- ljust() in python is a function that returns a left-justified string with a length equal to the specified width.
- It returns a new string of a given length after substituting a given character on the original string's right side.
- The fillchar character must be a single character. If we give fillchar more than one character, it will throw a TypeError.