Skip to content
String Methods

center()

Pad both sides so text is centred in a chosen width.

str.center(width, fillchar=' ')

str.center() returns a centered string of length width, padded with fillchar (default is a space).

If width is less than or equal to the string length, the original string is returned unaltered.

Parameters

NameDescription
widthThe total width of the returned string.
fillcharOne character to use for padding; it defaults to a space.

Returns

A centred string, padded to the requested width.

Try it

Run it and change it
word = "cat"
print(word.center(7, "-"))

Worth knowing

  • Single character fillchar: fillchar must be exactly one character long; passing an empty string or multi-character string raises TypeError.

Related entries