Python String Center() Method

Last Updated : 25 Apr 2026

Python center() method alligns string to the center by filling paddings left and right of the string. This method takes two parameters, first is a width and second is a fillchar which is optional. The fillchar is a character which is used to fill left and right padding of the string.

Syntax of String center() Method

It has the following syntax:

Parameters

  • width (required)
  • fillchar (optional)

Return Type

It returns modified string.

Examples of Python String center() Method

Here, we are going to take several examples to demonstrate the string center() Method in Python.

Example 1: default fill char

Here, we did not pass second parameter. By default it takes spaces.

Output:

Old value: Hello Tpointtech
New value:   Hello Tpointtech

Example 2: Formatting a String with center()

The following example demonstrates how the Python center() function places a string in the center of a specified width using a custom fill character.

Output:

Old value: Hello Tpointtech
New value: ##Hello Tpointtech##

Example 3: Using center() Function with Conditional Fill Characters

The following example demonstrates how the Python center() function can be used with an if-else condition to center a string using different fill characters based on a specified condition.

Output:

Old value: Hello Tpointtech
New value: !!Hello Tpointtech!!

Next TopicPython Strings