Description Of Python Center() Method

The center(n , 'char') character string method in Python returns a centered string in a string of length n by completing the right and left of the string with the 'char' character chosen as a parameter. The padding 'char' character is optional and in the ignored case the default padding is the space character.

Center() Method Parameters & Syntax

Syntax

string.center(width[, fillchar])

Parameters

  1. width: This is the total width of the string.
  2. fillchar: This is the fill character.

Example (filling using the '*' character)

s="Python Programming"
s = s.center(30, '*')
print(s)
# output: '******Python Programming******'

Example (filling using space character)

s="Python"
s = s.center(15)
print(s)
# output: ' Python '

Younes Derfoufi
my-courses.net
2 thoughts on “The Python center() method”

Leave a Reply