Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 center() Method

Python3 center() Method

Python3 Strings Python3 Strings

The center() method returns a string centered in a specified width; fillchar is the fill character and defaults to a space.

Syntax

Syntax of the center() method:


str.center(width[, fillchar])

Parameters

Return Value

Returns a string centered in the specified width; if width is smaller than the string width, the string is returned directly, otherwise fillchar is used for padding.

Example

The following example demonstrates the center() method:

Example

#!/usr/bin/python3

str

=

"[runoob]"

print

(

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

,

str

.

center

(

40

,

'*'

)

)

The output of the above example is:


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

Python3 Strings Python3 Strings