Python String ljust() Method
Python String ljust() method left aligns the string according to the width specified and fills remaining space of line with blank space if ‘fillchr‘ argument is not passed.
Syntax:
ljust(len, fillchr)
Parameters:
- len: The width of string to expand it.
- fillchr (optional): The character to fill in the remaining space.
Return Value:
Returns a new string of given length after substituting a given character in right side of original string.
Example 1
Python3
# example string string = 'gfg' width = 5 # print left justified string print (string.ljust(width)) |
Output:
gfg
Explanation:
Here, the minimum width defined is 5. So, the resultant string is of minimum length 5. And, the string ‘gfg’ is aligned to the left which makes leaves two spaces on the right of the word.
Example 2
Python3
# Python3 code to demonstrate # the working of ljust() lstr = "I love geeksforgeeks" # Printing the original string print ( "The original string is : \n" , lstr, "\n" ) # Printing the left aligned # string with "-" padding print ( "The left aligned string is : " ) print (lstr.ljust( 40 , '-' )) |
Output:
The original string is : I love geeksforgeeks The left aligned string is : I love geeksforgeeks--------------------