String Methods
center()
Pad both sides so text is centred in a chosen width.
str.center(width, fillchar=' ')
str.center() returns a centered string of length width, padded with fillchar (default is a space).
If width is less than or equal to the string length, the original string is returned unaltered.
Parameters
| Name | Description |
|---|---|
| width | The total width of the returned string. |
| fillchar | One character to use for padding; it defaults to a space. |
Returns
A centred string, padded to the requested width.
Try it
Run it and change it
word = "cat" print(word.center(7, "-"))
Worth knowing
- Single character fillchar:
fillcharmust be exactly one character long; passing an empty string or multi-character string raisesTypeError.