Easy Tutorial
❮ Python Number Python Heap Sort ❯

Python3 center() Method

Python3 Strings

The center() method returns a string centered in a specified width (width), with an optional fill character (fillchar), which defaults to a space.

Syntax

The syntax for the center() method is:

str.center(width[, fillchar])

Parameters

Return Value

Returns a string centered within the specified width (width). If the width is less than the length of the string, the original string is returned. Otherwise, the fillchar is used for padding.

Example

The following example demonstrates the center() method:

#!/usr/bin/python3

str = "[tutorialpro]"

print("str.center(40, '*') : ", str.center(40, '*'))

The output of the above example is:

str.center(40, '*') :  ****************[tutorialpro]****************

Python3 Strings

❮ Python Number Python Heap Sort ❯