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

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python string center() usage and example

Python string methods

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

The syntax of the center() method is:

string.center(width[, fillchar])

center() parameters

The center() method takes two parameters:

  • width- 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: center() method with default fillchar parameter

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

When running the program, the output is:

Filled string:             Python is awesome

Example2: fillchar is* of the center() method

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

When running the program, the output is:

Filled string:  ***Python is awesome****

Python string methods