Python String center() Method

Returns center-aligned string


The center() method returns center-aligned string of length width.

Padding is done using the specified fillchar (default is an ASCII space).

The original string is returned as it is, if width is less than or equal to string length.


Python String center() method parameters
widthRequiredThe length of the string
fillcharOptionalA character you want to use as a fill character.
Default is an ASCII space.

Basic Example

# Align text center
S = 'Centered'
x =

# Prints    Centered   

Specify a Fill Character

By default the string is padded with whitespace (ASCII space).

You can modify that by specifying a fill character.

# center() with '*' as a fill character
S = 'Centered'
x =, '*')

# Prints ***Centered***

Equivalent Method

You can achieve the same result by using format() method.

# Align text center with format()
S = 'Centered'
x = '{:*^14}'.format(S)

# Prints ***Centered***