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

Python Basic Tutorial

Python Flow Control

Função do Python

Tipos de Dados do Python

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 that is centered and filled with spaces to the length width.

center() method syntax

string.center(width[, fillchar])

center() parameters

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