ljust()

Add left justified filler strings of specified width
my_string.ljust(width, filler)
width : left justified string of width
filler : optional, string can be used to fill the width
my_str='Welcome to'
output=my_str.ljust(15,'*')
print(output) # Welcome to*****

my_str='Welcome to'
output=my_str.ljust(5,'*')
print(output) # Welcome to

Handling Strings Longer Than Width

If the string is longer than the specified width, ljust() leaves it unchanged:

my_str = 'Python'
print(my_str.ljust(4, '*'))  # Output: 'Python'

Use Case: Formatting Table Output

You can use ljust() to format columns in a table:

header = "Name".ljust(10) + "Age".ljust(5)
row = "Alice".ljust(10) + "25".ljust(5)
print(header)
print(row)
Name      Age  
Alice     25  

Using Different Filler Characters

ljust() can fill extra space with any character:

text = 'Data'
print(text.ljust(10, '-'))  # Output: 'Data------'

Use Case: Generating Console Output

Aligning text output for a CLI application:

item = "Product".ljust(15, '.') + "Price"
print(item) # Output: 'Product.......Price'

Use Case: String Alignment in Logs

Aligning log entries for readability:

log_entry = "INFO".ljust(8) + "Operation completed."
print(log_entry)# Output: 'INFO    Operation completed.'

All String methods rjust()


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer