Ljust() Method Of Str

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:

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:

Flowers are ephemeral####

Roses are red@@@@@@@

4302782512

4302782512

 


Copyright 2019 © pythontic.com