Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python String ljust() Method

  • Last Updated : 05 Oct, 2021

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--------------------

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!