Str.center will return a centered string of length width and padding is done using the specified fill character.

Syntax of Str.center:

center(width, fillchar=' ') By default it is space (if not filled anything)

a='Apple'

a.center(9)

Output:

' Apple '

It adds spaces to both side. Similarly we can pad any char by filling it in fill char.

use str.center without filling fill char
Str.center-without Fill char

Example-2: By filling character.

a='Apple'

a.center(9,'-')

Output:

'--Apple--'

str.center using fill char
Str.center-Using Fill char

 

By SC

One thought on “Str.center”

Comments are closed.