Python ljust() Method

Python 字符串Python String


Description

The Python ljust() method returns a new string that is left-aligned with the original string and padded with spaces to the specified length. If the specified length is less than the length of the original string, the original string is returned.

Syntax

Syntax of the ljust() method:

str.ljust(width[, fillchar])

Parameters

  • width -- The specified string length.
  • fillchar -- The fill character, default is a space.

Return Value

Returns a new string that is left-aligned with the original string and padded with spaces to the specified length. If the specified length is less than the length of the original string, the original string is returned.

Example

The following example demonstrates the usage of ljust():

#!/usr/bin/python

str = "this is string example....wow!!!";

print str.ljust(50, '0');

The output of the above example is:

this is string example....wow!!!000000000000000000

Python 字符串Python String

Other Extensions