all Python 3Strings (str)function
str.center
alignment
Center the string in a field of the given width, padded with fillchar (space by default).
Syntax
s.center(width[, fillchar])
Width only pads, it never trims: if width is not greater than the string length you simply get the string back, so center alone does not guarantee aligned columns. When the padding does not split evenly, center puts the extra character on the left (for an odd width), whereas the format spec f'{s:^6}' always puts it on the right — so the two ways of centering can produce different strings.
Examples
print('hi'.center(6, '*')) # → **hi**
print('hi'.center(5)) # → hi
print('hi'.center(1)) # → hi
See also
Open in the full glossarydocs.python.orgReport an inaccuracy