Python String center()

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

The syntax of center() method is:

string.center(width[, fillchar])

center() Parameters

The center() method takes two arguments:

  • width - length of the string with padded characters
  • fillchar (optional) - padding character

The fillchar argument is optional. If it's not provided, space is taken as default argument.


Return Value from center()

The center() method returns a string padded with specified fillchar. It doesn't modify the original string.


Example 1: center() Method With Default fillchar

string = "Python is awesome"

new_string = string.center(24)

print("Centered String: ", new_string)

When you run the program, the output will be:

Centered String:     Python is awesome      

Example 2: center() Method With * fillchar

string = "Python is awesome"

new_string = string.center(24, '*')

print("Centered String: ", new_string)

When you run the program, the output will be:

Centered String:  ***Python is awesome****