Python center() Method

Python 字符串Python String


Description

Python center() returns a new string with the original string centered and padded with spaces to a length of width. The default padding character is a space.

Syntax

Syntax of the center() method:

str.center(width[, fillchar])

Parameters

  • width -- The total width of the string.
  • fillchar -- The fill character.

Return Value

This method returns a new string with the original string centered and padded with spaces to a length of width.

Example

The following example demonstrates the center() method:

>>>str = 'example' >>> str.center(20, '*') '*******example*******' >>> str.center(20) ' example ' >>>

Python 字符串Python String

Other Extensions