Python center() Method

Definition
In Python, center() returns a new string with the text centered in a field of a given width. Extra space is filled with a padding character.
Width is required. fillchar is optional and defaults to a space. It must be exactly one character.
Syntax
Call center with a width. Start with this line.
banner = text.center(20, "-")
| Call | What it does |
|---|
"hi".center(6) | Pads with spaces to width 6. |
"hi".center(6, "*") | Pads with "*" on both sides. |
If width is less than or equal to len(text), the original string is returned unchanged.
Library declarations
str.center(width, fillchar=" ") → str
Parameters
| Parameter | Description |
|---|
| width | Required. Total length of the returned string. |
| fillchar | Optional. One character used for padding. Default is a space. |
Return Value
| Return Value | Description |
|---|
| str | A new centered string, or the original if width is too small. |
Python Examples
Example 1 — Basic center() with Fillchar
Center a short word with a custom padding character and check the length.
original = "Python"
centered = original.center(10, "*")
print("Original:", original)
print("Centered:", centered)
print("Length: ", len(centered))
Original: Python
Centered: **Python**
Length: 10
How It Works
- Measure the text.
"Python" is 6 characters. - Compute padding. Width 10 needs 4 extra characters.
- Split evenly. Two
* on the left, two on the right. - Original untouched. Immutability keeps
original as "Python".
Example 2 — Header Banner
Build a dashed title line; odd leftover padding lands on the right.
title = "Welcome to Python"
banner = title.center(30, "-")
print(banner)
print("Length:", len(banner))
------Welcome to Python-------
Length: 30
How It Works
- Title length.
"Welcome to Python" is 17 characters. - Padding total.
30 - 17 = 13 dashes to place. - Left first.
13 // 2 = 6 dashes on the left. - Right gets the leftover.
7 dashes on the right (odd pad rule).
Example 3 — center() vs ljust() / rjust()
Same text and width—three different alignments.
word = "Data"
width = 12
print("center:", repr(word.center(width, ".")))
print("ljust: ", repr(word.ljust(width, ".")))
print("rjust: ", repr(word.rjust(width, ".")))
center: '....Data....'
ljust: 'Data........'
rjust: '........Data'
How It Works
- center. Padding splits on both sides → middle alignment.
- ljust. Text stays left; dots fill the right.
- rjust. Text stays right; dots fill the left.
- repr(). Makes every padding character visible in the output.
Frequently Asked Questions
It centers text inside a field of a given width using a fill character.
width is required. fillchar is optional and defaults to a space.
center() returns the original string with no padding.
Exactly one character. A multi-character string raises TypeError.
center() pads both sides. ljust() pads on the right. rjust() pads on the left.
Next: count()
Count how many times a substring appears in a string.
count() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful