Python Reference Library →

Python String center() Method – Center Align a String with Padding

Let’s look at string center() method – center align a string with padding

  1. Notice

    center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea'
result = text.center(8, ".")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · str'..tea...'
This step

center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.

Picture string center() method – center align a string with padding

center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.

Image brief

Alt text
A diagram for Python String center() Method – Center Align a String with Padding. Instructions: text = 'tea' result = text.center(8, ".") print(result). Displayed result: ..tea.... The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea'; object 2: '..tea...'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String center() Method – Center Align a String with Padding". Teaching focus: center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea' result = text.center(8, ".") print(result) | v DISPLAYED RESULT ..tea... FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'tea' object 2: '..tea...' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String center() Method – Center Align a String with Padding. Instructions: text = 'tea' result = text.center(8, ".") print(result). Displayed result: ..tea.... The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea'; object 2: '..tea...'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea'
result = text.center(8, ".")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · str'..tea...'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
..tea...

What do you expect this program to print?

What does this example display?

text = 'tea'
result = text.center(8, ".")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

..tea...

Follow the printed values in order. The output is: ..tea...

More examples and reference

Python String center() Method

The center() method in Python returns a new string of a given length with the original string center-aligned, and padded with spaces or a specified fill character on both sides.

Syntax

string.center(width, fillchar=' ')

Parameters:

  • width – Total length of the final string (including the original string and padding)
  • fillchar (optional) – The character used for padding (default is space)

Returns:

  • A new centered string of specified width

Example 1: Center a String with Spaces

text = "Python"
centered = text.center(10)
print(centered)
  Python  

Why? The original string is 6 characters long. To make it 10 characters wide, 2 spaces are added to each side.

Example 2: Center a String with Custom Character

text = "Python"
centered = text.center(12, '*')
print(centered)
***Python***

Note: If the number of padding characters is odd, the extra character is placed on the right side.

Use Case: Formatting Output

Centering is often used when displaying titles or headers in reports or CLI tools.

title = "Welcome"
print(title.center(30, '='))
===========Welcome===========

Common Mistakes

  • Using a multi-character string for fillchar will raise a TypeError
  • Passing a non-integer value for width will also raise an error

Interview Tip

If asked to format text output in coding interviews, methods like center(), ljust(), and rjust() are great tools to mention.

Summary

  • center() centers a string in a field of a given width
  • Spaces are used for padding by default, but you can specify a different character
  • Useful for text formatting in CLI or reports

Practice Problem

Write a program that takes a user's name and displays it centered within a line of asterisks:

name = input("Enter your name: ")
print(name.center(30, '*'))

What to remember

Connect what you noticed in the example with the idea below.

Remember

center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.

Watch out for this idea

center() truncates text that is longer than the width.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →