Python String Method – Center()

Spread the love

Center() method –

The center method returns a new centered string after padding the string with specified characters on both ends.

Syntax –

string.center(width, fillchar)

Width – the width of the string with padding.

fillchar ( optional ) – the padding character. By default it is white space if not specified.

Example –

In [1]: s1 = "sirens of the sea"

In [2]: s1.center(30)
Out[2]: '      sirens of the sea       '

In [3]: s1.center(30, '*')
Out[3]: '******sirens of the sea*******'

The center method does not change the original string.

In [4]: s1
Out[4]: 'sirens of the sea'

Related Posts –

  1. Python String Method – Capitalize()
  2. Python String Method – title()

Rating: 1 out of 5.

Leave a Reply