Python String center() Method

Last Updated:

Python String center()

The center() method returns a string which is padded with the specified character.

Example

Python Compiler
txt = "wikimass" x = txt.center(24) print(x)

Syntax

txt.center(width, fill)

Parameter Values

ValueTypeExplanation
width Required Specifies the total width of the string.
fill Optional Specifies the filler character.
Default value is " " (whitespace)

Return Value

ValueExplanation
New string Returns a string which is padded with the specified character.
Org string Returns the original string, if width is less than or equal to length of the string.

More Example

In the following example, we use "-" as the padding character.

Example

Python Compiler
txt = "wikimass" x = txt.center(16, "-") print(x)

Share this Page

Meet the Author