Home >>Python String Methods >Python String center() Method

Python String center() Method

Python String center() Method

Python string center() method is used to align the string in center using a specified character whose default value is space as the fill character.

Syntax:
string.center(length, character)

Parameter Values

Parameter Description
length This is a required parameter. It defines the length of the returned string.
character This is an optional parameter. It defines the character to fill the missing space on each side.
Here is an example of Python center() method:

txt = "PHPTPOINT"
x = txt.center(20)
print(x)

Output:
PHPTPOINT
Example 2:

txt = "PHPTPOINT"
x = txt.center(20, "*")
print(x)

Output:
*****PHPTPOINT******