all Python 3Byte sequencesfunction
bytearray.center
bytearray — padding
Center in a field of the given width using a fill byte.
Syntax
ba.center(...)
The fill must be a bytes object exactly one byte long (b'*'); passing the str '*' raises TypeError, which is the usual stumble when moving over from strings. And although bytearray is mutable, this method changes nothing in place: it returns a new object, and when width is not greater than the current length you simply get an unpadded copy.
Examples
print(bytearray(b'hi').center(6, b'*')) # → bytearray(b'**hi**')
print(bytearray(b'hi').center(6)) # → bytearray(b' hi ')
print(bytearray(b'ok').center(10, b'.').decode()) # → ....ok....
print(bytearray(b'hi').center(5, b'-')) # → bytearray(b'--hi-')
print(bytearray(b'hello').center(3, b'*')) # → bytearray(b'hello')
print(bytearray(b'hi').center(6, b'**')) # → TypeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy