/  Center() method in Python String Module

Center() method in Python String Module

Center method in Python program lengthens the string by adding defined or specified characters.


Syntax:

str.center(width[,  fillchar])


Parameters:

It has two arguments width and fillchar.

  • Width: lengthens the string by adding defined or specified characters.
  • Fillchar: This is an optional character to lengthen the string. If this is not given then space will be taken as default argument.


Example-1:

Let us use the method with default fillchar

text= 'Lucerne is in Switzerland'
x = text.center(45)
print(x)


Output:

“          Lucerne is in Switzerland         ”


Example-2:

Let us use the method with fillchar  ‘/’

txt= 'Lucerne is in Switzerland'
x = txt.center(42, '/')
print(x)


Output: