Tutorial Study Image

Python ljust()

The ljust() function in python helps to align the string to left by filling specified characters with the given minimum length. Space is considered as the default fill character in case of missing fillchar parameter.

string.ljust(width[, fillchar]) #where width is an integer value

ljust() Parameters:

The ljust() function takes two parameters. The fillchar parameter must be a single character. If the given fillchar parameter has more than one character then it will throw a TypeError.

Parameter Description Required / Optional
width width of the returned string Required
fillchar A character to fill the missing space Optional

ljust() Return Value

The return value is always a string. Here the fillchar character is filled in the right of the string. If the specified width is less than or equal to the length of the string, then the original string is returned as output.

Input Return Value
If parameters left-justified string

Examples of ljust() method in Python

Example 1: Ho to left justify string of minimum width in Python?

# example string
string = 'Python'
length = 7

# print left justified string



Example 2: How to left justify string and fill the remaining spaces in Python?

# example string
string = 'Python'
length = 10
fill = '*'

# print left justified string
print(string.ljust(length, fill))