Python String ljust() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String ljust() method

The ljust() method in Python left aligns the string, using a specific number of characters (space is used as default) as the provided fill character.

Syntax of ljust() method
string.ljust(length, fill_character)

length:         The length of the output string(Required).
fill_character: A character to fill the missing space 
                to the right of the string(Optional).
                Default is space.     
Example 1

Code

x = "Pylenin"
y = x.ljust(20)
print(len(y))
print(y)

Output

20
Pylenin         

As you can see, the length of the new string is 20 characters long.

Example 2 - Using a different fill character

Code

x = "Pylenin"
y = x.ljust(20, "*")
print(len(y))
print(y)

Output

20
Pylenin*************        

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide