English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python string center() usage and examples

Python string methods

The center() method returns a new string with the original string centered and filled with spaces to the length width.

The syntax of the center() method is:

string.center(width[, fillchar])

center() parameters

The center() method takes two parameters:

  • width- The length of the filled string

  • fillchar (Optional)-Fill character

The fillchar parameter is optional. If not provided, a space is used as the default parameter.

center() return value

The center() method returns a string filled with the specified fillchar. It does not modify the original string.

Example1: The center() method with the default fillchar parameter

string = "Python is awesome"
new_string = string.center(24)
print("The filled string: ", new_string)

The output when running the program is:

The filled string:       Python is awesome

Example2: fillchar is* the center() method

string = "Python is awesome"
new_string = string.center(24, ''*)
print("The filled string: ", new_string)

The output when running the program is:

The filled string:  ***Python is awesome****

Python string methods