Last Updated : 23 Jul, 2025
Python ljust() method is used to left-justify a string, padding it with a specified character (space by default) to reach a desired width. This method is particularly useful when we want to align text in a consistent format, such as in tables or formatted outputs.
Here's a basic example of how to use ljust() method:
Python
s1 = "Hello"
s2 = s1.ljust(10)
print(s2)
Explanation:
Parameters:string.ljust(width, fillchar=' ')
The ljust()
method can be used without specifying a fillchar
, in which case it defaults to a space.
s = "Python"
# Left-aligning the string with a total width of 12
res = s.ljust(12)
print(res)
Explanation:
s
is left-aligned in a field of width 12.Sometimes, we may want to use a character other than a space to fill the remaining space. The ljust()
method allows you to specify a custom fillchar
.
s = "Data"
# Left-aligning the string with
# a width of 10 and using '-' as the fill character
res = s.ljust(10, '-')
print(res)
Explanation:
fillchar
parameter is set to '-'
, so the remaining space is filled with dashes.ljust()
with other string methods
ljust()
method can also be used alongside other string methods to achieve more complex formatting.
s = "Align"
# Left-aligning the string and converting it to uppercase
res = s.ljust(10).upper()
print(res)
Explanation:
s
is left-aligned in a field of width 10..upper()
method.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4