str.center()

Center a string in a fixed-width field — a copy is returned; the original is unchanged. Never truncates.

String methodPython 1.0+Live demo
Common call
"hi".center(10)
Returns
new str, length ≥ width
Replaces
sprintf("%*s") style formatting
Watch out
odd padding leans LEFT; width ≤ len returns the original untouched
str.center(widthwidthTarget minimum length. Shorter strings are padded; longer or equal ones are returned unchanged.type: int · required, fillcharfillcharSingle-character string used for padding. Multi-char or empty raises TypeError.type: str · default: " "=" ")
str

Demo

Live evaluation
Try:
Inputs
stringstrthe source
widthinttarget width
fillcharstrempty = space
Output
'hi'.center(8)
' hi '

center pads the original on BOTH sides until the total length equals `width`. When the padding is odd, the extra character goes on the LEFT (e.g. "hi".center(5) → " hi "). If the string is already at or past `width`, the original is returned unchanged — center never truncates. fillchar must be exactly one character; empty falls back to the default space.

Parameters

NameTypeRequiredDescription
widthintyesTarget minimum length. Shorter strings are padded; longer or equal ones are returned unchanged.
fillcharstrno (" ")Single-character string used for padding. Multi-char or empty raises TypeError.

Return value

strA copy of the string centered in a field of at least `width` characters, padded on both sides with `fillchar`. If odd padding is needed, the extra character goes on the LEFT.

Common patterns

Section headers
Bracket a title with fill characters — great for text-mode banners.
print(" README ".center(60, "="))
Tabular labels
Fixed-width centered column labels.
header = "".join(col.center(12) for col in columns)
Aligned CLI output
Use center alongside ljust/rjust to lay out mixed-alignment rows.
print(name.ljust(20) + status.center(10) + count.rjust(6))

Examples

1. Even padding
"hi".center(8)
Returns
" hi "
2. Odd → left
"hi".center(7)
Returns
" hi " # extra space on the left
3. With fill char
"title".center(15, "-")
Returns
"-----title-----"
4. No padding needed
"exact".center(5)
Returns
"exact"
5. Never truncates
"long text".center(3)
Returns
"long text" # returned unchanged
6. Empty source
"".center(5, "*")
Returns
"*****"

Pitfalls

1. Odd padding leans LEFT, not right
When (width - len) is odd, the extra padding character goes on the LEFT side. Consistent, but surprising if you expected symmetric-visual bias toward right.
Not centered visually
"hi".center(5)
" hi " # left has one more space than right
Read as: ceil left, floor right
right = (width - len(s)) // 2
left  = width - len(s) - right
left >= right
2. Never truncates
width ≤ len returns the original unchanged, not a shortened version. Great as a "pad only if needed" primitive; wrong when you actually want a fixed length.
Overflow
"long text".center(3)
"long text" # 9 chars, not 3
Slice first
s[:width].center(width)
true fixed width
3. fillchar must be exactly one character
Empty or multi-character fillchar raises TypeError. Emoji that are single codepoints work; grapheme clusters (emoji + modifier) do not.
Multi-char fill
"hi".center(10, "-=")
TypeError: The fill character must be exactly one character long
One character
"hi".center(10, "-")
"----hi----"
4. Original string is NOT modified
Like all string methods, center returns a new string. Assigning it back is required for the padded value to persist.
Lost result
s = "hi"
s.center(10)
print(s)
"hi" # unchanged
Capture it
s = s.center(10)
print(s)
" hi "

When to use

Use it
  • Centered labels in text UIs and CLI banners
  • Tabular output where visual center matters
  • Padding-only-if-needed workflows
  • Rendering fixed-width report columns
Reach for something else
  • Numeric columns → rjust reads better for right-aligned numbers
  • Left-aligned columns → ljust
  • You need to truncate too → slice first, then center
  • You need padding on one side only → ljust or rjust

Notes

Complexity
O(width)
Return
A new string; the original is unchanged (strings are immutable)
CPython impl
Objects/unicodeobject.c :: pad — with computed left/right halves
Memory
Allocates one new string of length max(len, width)
Thread-safe
Yes — strings are immutable

FAQ

CPython uses floor(padding / 2) on the right and ceil(padding / 2) on the left. This makes center() deterministic — Python has consistently biased extra padding toward the left since the method was introduced.

History

1.0
center() has been part of str since Python 1.0.
2.4
fillchar parameter added.