Python3 center() Method

Python3 字符串Python3 Strings

The center() method returns a string centered to a specified width. fillchar is the fill character, which defaults to 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

Returns a string centered to a specified width. If width is less than the width of the string, the string is returned directly; otherwise, it is filled with fillchar.

Example

The following example demonstrates the use of the center() method:

Example

#!/usr/bin/python3

str = "[example]"

print ("str.center(40, '*') : ", str.center(40, '*'))

The output of the above example is as follows:

str.center(40, '*') :  ****************[example]****************

Python3 字符串Python3 Strings

Other Extensions