String Methods

str.center()

Pad on both sides to center the text. Odd padding need not be symmetric; the result is never truncated. Width measures code points, not terminal cells or grapheme clusters. The original string is unchanged.

Formal Syntax & Structure

python syntaxpython
text.center(width, fillchar=" ", /)

Parameters & Attributes

NameTypeDescription
widthintMinimum result length in Unicode code points; accepts index-compatible integers.
fillcharstrExactly one code point; defaults to ASCII space.

Return Value

str

Padded text, or unchanged text when width is no larger than its length.

Working Example

Example Snippetpython
print("API".center(7, "-"))print("سلام".center(8, "."))
Python Reference