Python center() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

python
banner = text.center(20, "-")
CallWhat 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
python
str.center(width, fillchar=" ") → str

Parameters

ParameterDescription
widthRequired. Total length of the returned string.
fillcharOptional. One character used for padding. Default is a space.

Return Value

Return ValueDescription
strA 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.

python
original = "Python"
centered = original.center(10, "*")

print("Original:", original)
print("Centered:", centered)
print("Length:  ", len(centered))

How It Works

  1. Measure the text. "Python" is 6 characters.
  2. Compute padding. Width 10 needs 4 extra characters.
  3. Split evenly. Two * on the left, two on the right.
  4. Original untouched. Immutability keeps original as "Python".

Example 2 — Header Banner

Build a dashed title line; odd leftover padding lands on the right.

python
title = "Welcome to Python"
banner = title.center(30, "-")

print(banner)
print("Length:", len(banner))

How It Works

  1. Title length. "Welcome to Python" is 17 characters.
  2. Padding total. 30 - 17 = 13 dashes to place.
  3. Left first. 13 // 2 = 6 dashes on the left.
  4. 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.

python
word = "Data"
width = 12

print("center:", repr(word.center(width, ".")))
print("ljust: ", repr(word.ljust(width, ".")))
print("rjust: ", repr(word.rjust(width, ".")))

How It Works

  1. center. Padding splits on both sides → middle alignment.
  2. ljust. Text stays left; dots fill the right.
  3. rjust. Text stays right; dots fill the left.
  4. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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