The center() method returns a new string with the original text centered inside a field of a specified width. Padding is added on both sides—using spaces by default, or any single character you choose. It is a simple way to format headers, console output, and fixed-width columns.
01
Center Align
Place text in the middle.
02
Width Parameter
Set total string length.
03
fillchar
Custom padding character.
04
Immutable
Original string stays unchanged.
05
Headers
Format titles and banners.
06
vs ljust/rjust
Pick the right alignment.
Fundamentals
Definition and Usage
In Python, center() is a built-in string method that pads the string on the left and right so the original text sits in the middle of a field with total length width. For example, "Python".center(10, "*") returns "**Python**"— two asterisks on each side to reach a total width of 10 characters.
💡
Beginner Tip
Think of width as the total size of the final string, not the amount of padding to add. If the text is already longer than width, Python returns the original string with no changes.
Foundation
📝 Syntax
The center() method accepts one required and one optional parameter:
python
string.center(width, fillchar)
Syntax Rules
width — the total length of the returned string (required, integer).
fillchar — optional single character used for padding; defaults to a space (" ").
Return value — a new str; the original is never modified.
Width too small — if width <= len(string), the original string is returned unchanged.
Odd padding — when extra space does not split evenly, the left side gets one extra character.
Cheat Sheet
⚡ Quick Reference
Expression
Result
"Python".center(10, "*")
"**Python**"
"Hi".center(6)
" Hi " (spaces)
"Hello".center(3)
"Hello" (unchanged)
"A".center(4, "-")
"-A--"
"".center(5, ".")
"....."
Basic
"title".center(20, "-")
Centered banner line
Default pad
"data".center(10)
Space padding
Column
name.center(15)
Fixed-width field
Length
len("Hi".center(8))
Returns 8
Hands-On
Examples Gallery
Run these examples in any Python 3 interpreter. Each one shows a common center-alignment pattern.
📚 Getting Started
Start with a short word and pad it to a fixed width using a custom fill character.
Example 1 — Basic center() with fillchar
Center "Python" inside a field of 10 characters, using asterisks as padding.
ljust() keeps text on the left and pads the right.
rjust() keeps text on the right and pads the left.
Applications
🚀 Common Use Cases
Console banners — center titles and section headers with dash or star padding.
Report columns — align numeric or short text values in fixed-width fields.
Receipt formatting — center store names or headings on printed lines.
CLI menus — highlight the active menu title in the middle of the screen width.
Debug output — visually separate log sections with centered divider lines.
🧠 How center() Works
1
Python checks the width
If width is not larger than the string length, the original string is returned as-is.
Guard
2
Padding is calculated
Python computes how many fillchar characters are needed and splits them between left and right sides.
Pad
3
A new string is built
Left padding + original text + right padding are joined into one string of exactly width characters.
Build
=
📐
Centered output
Text sits in the middle of a fixed-width field, ready to print or display.
Important
📝 Notes
fillchar must be exactly one character. Passing "--" raises TypeError.
When padding does not divide evenly, the extra character goes on the left side.
center() counts string length in characters, not display width—emoji and some Unicode may appear wider in terminals.
For left or right alignment instead, use ljust() or rjust().
Wrap Up
Conclusion
The center() method is a straightforward way to center-align text in Python. With just a width and an optional fill character, you can format headers, columns, and banners without manual space counting or string concatenation.
Remember: width is the total size of the result, padding splits left and right, and if the text is already long enough, nothing changes.
Pick a width that fits your column or screen layout
Use a visible fillchar like "-" for banners
Use repr() when debugging to see hidden spaces
Combine with strip() before centering user input
Compare ljust() and rjust() when center is not needed
❌ Don’t
Pass a multi-character string as fillchar
Assume width means “padding amount” only
Expect the original string to change in place
Use center() for complex table layouts—consider formatted printing
Forget that odd padding adds an extra character on the left
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about center()
Use these points when formatting aligned text in Python.
5
Core concepts
📐01
Center Align
Text in the middle.
Purpose
🔢02
width Param
Total result length.
Syntax
⬜03
fillchar
One pad character.
Optional
📄04
Banners
Great for headers.
Use case
⚠05
Too Narrow
No change if too short.
Edge case
❓ Frequently Asked Questions
center() returns a new string with the original text centered inside a field of a given width. Extra space on the left and right is filled with a padding character, which defaults to a space.
string.center(width, fillchar). width is required and sets the total length of the returned string. fillchar is optional and defaults to a single space character.
If width is less than or equal to the length of the original string, center() returns the original string unchanged. No padding is added.
fillchar must be exactly one character—a letter, digit, symbol, or space. For example, "-" or "*". Passing a multi-character string raises TypeError.
No. Python strings are immutable. center() always returns a new string; the original stays the same.
center() adds padding on both sides so the text sits in the middle. ljust() pads on the right (left-aligned), and rjust() pads on the left (right-aligned).