ljust() method of str

ljust() left justifies a string inside a string of width n.When width is more than the length of original string it is padded with specified characters

Str Updated 12 Mar 2017 1 min read

Method Name

ljust

Method Signature

ljust(width[, fillchar])

Method Overview

  • ljust(), left justifies a string of width 'n' and returns the new string.

  • When the original string length exceeds the specified width, string is padded with the fillchar for the remaining width.

  • When the width is less than or equal to the length of the original string, the original string is returned.

Example

python
stmt = "Flowers are ephemeral"

leftJustified = stmt.ljust(25,"#")

print(leftJustified)

stmt = "Roses are red"

leftJustified = stmt.ljust(20,"@")

print(leftJustified)

stmt1 = stmt.ljust(5)

print(id(stmt1))

print(id(stmt))

Output

Output
Flowers are ephemeral####

Roses are red@@@@@@@

4302782512

4302782512