Python String center() Method
Centers a string to fit in the specified width, with an optional fill character. If the string is longer than the specified width, the string is returned without being centered.
Syntax
Python
Copy Code
string.center(length, character = " ")
Parameters
Parameter | Description |
---|---|
length |
Required. The length of the string to be returned |
character |
Optional. The character to use when padding out each side of the supplied string. The default is a space (" ") |
Example
Python
Copy Code
print('abc'.center(12)) print('abc'.center(12, '-')) print('This string is longer than 12 characters.'.center(12, '='))
Output
abc ----abc----- This string is longer than 12 characters.