Home » Python String | center() method with Examples

Python String | center() method with Examples

by Online Tutorials Library

Python String Center() Method

Python center() method alligns string to the center by filling paddings left and right of the string. This method takes two parameters, first is a width and second is a fillchar which is optional. The fillchar is a character which is used to fill left and right padding of the string.

Signature

Parameters

  • width (required)
  • fillchar (optional)

Return Type

It returns modified string.

Python String Center() Method Example 1:default fill char

Here, we did not pass second parameter. By default it takes spaces.

Output:

Old value: Hello TutorAspire New value:   Hello TutorAspire 

Python String Center() Method Example 2

Here, we are providing padding char (optional) parameter as #. See the example.

Output:

Old value: Hello TutorAspire New value: ##Hello TutorAspire ##  

Python String Center() Method Example 3

Output:

Old value: Hello TutorAspire New value: !!Hello TutorAspire!!  

Next TopicPython Strings

You may also like