Python3 center() Method
The center() method returns a string centered in a specified width; fillchar is the fill character and defaults to a space.
Syntax
Syntax of the center() method:
str.center(width[, fillchar])
Parameters
- width -- the total width of the string.
- fillchar -- the fill character.
Return Value
Returns a string centered in the specified width; if width is smaller than the string width, the string is returned directly, otherwise fillchar is used for padding.
Example
The following example demonstrates the center() method:
Example
#!/usr/bin/python3
str
=
"[runoob]"
(
"str.center(40, '*') : "
,
str
.
center
(
40
,
'*'
)
)
The output of the above example is:
str.center(40, '*') : ****************[runoob]****************
Python3 Strings