Python string alignment ljust method

Python String Alignment: ljust Method

Description

The ljust() method returns a left-aligned string with the specified width. It is padded with the specified fill character (space by default). If width is less than the length of s, the original string is returned.

Syntax

The syntax of the ljust() method is as follows:

var.ljust(width[, fillchar])

Parameters

  • width – This is the total string length after padding.
  • fillchar – This is the fill character, which defaults to space.

Return Value

This method returns a left-justified string with the specified width. It is padded with the specified padding character (space by default). If the width is less than the length of s, the original string is returned.

Example

The following example demonstrates the use of the ljust() method.

var = "This Is String Example....WOW!!!"
var1 = var.ljust(40, '*')

print ("original string:", var)
print ("Left justified string:", var1)

When you run this program, the following output will be produced:

original string: This Is String Example....WOW!!!
Left justified string: This Is String Example....WOW!!!*******

Leave a Reply

Your email address will not be published. Required fields are marked *