A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-string-ljust-method/ below:

Python String ljust() Method - GeeksforGeeks

Python String ljust() Method

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:

Syntax of ljust() method

string.ljust(width, fillchar=' ')

Parameters: Return Type: Using the default fill character

The ljust() method can be used without specifying a fillchar, in which case it defaults to a space.

Python
s = "Python"
# Left-aligning the string with a total width of 12
res = s.ljust(12)
print(res)    
Explanation: Specifying a custom fill character

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.

Python
s = "Data"

# Left-aligning the string with 
# a width of 10 and using '-' as the fill character
res = s.ljust(10, '-')
print(res)  
Explanation: Combining ljust() with other string methods

ljust() method can also be used alongside other string methods to achieve more complex formatting.

Python
s = "Align"

# Left-aligning the string and converting it to uppercase
res = s.ljust(10).upper()
print(res) 
Explanation:

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