bytes.center(width, fillbyte) returns a copy of the data padded on both sides to width bytes. The default fill is an ASCII space; any single byte can be used instead.
When the padding cannot be split evenly, one side gets an extra byte. If width is not larger than the data, the data is returned unchanged; it is never truncated.
bytes.center() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.center(...). No import is needed, and bytearray objects have the same method.
Syntax
Give the total width and, optionally, a one-byte fill.
python
result = data.center(width)
result = data.center(width, fillbyte)
Call
What it does
b"ok".center(6)
Returns b' ok '.
b"ok".center(6, b"*")
Returns b'**ok**'.
b"long".center(2)
Returns b'long'.
The fill must be a bytes or bytearray object of length exactly 1.
Method signatures
python
bytes.center(width, fillbyte=b' ', /)
# returns a new bytes object (bytearray.center() returns a bytearray)
Parameters
Parameter
Description
width
The total length of the result in bytes.
fillbyte
Optional. A single byte used for padding. Default b" ".
Return Value
Return Value
Description
bytes
The data centered in a field of width bytes, or a copy of the data if it is already that long.
Python Examples
Example 1 — Center a Menu Title
Center the same title with spaces, with stars, in an odd width, and in a width that is too small.
python
title = b"MENU"
print(title.center(10))
print(title.center(10, b"*"))
print(title.center(9, b"-"))
print(title.center(2))
Output
b' MENU '
b'***MENU***'
b'---MENU--'
b'MENU'
How It Works
Even padding. Six spare bytes are split three and three.
Custom fill.b"*" replaces the spaces.
Odd padding. Five spare bytes cannot be split evenly; with this width the extra byte goes on the left.
Too narrow. A width smaller than the data returns it unchanged.
Example 2 — Fill a 16-Character Display Line
Character LCDs often have 16 columns. Center each message in exactly 16 bytes so old text is fully overwritten.
python
WIDTH = 16
for msg in [b"READY", b"TEMP 21C", b"ERR"]:
line = msg.center(WIDTH, b".")
print(line, len(line))
Constant width. Every line is exactly 16 bytes, whatever the message length.
Dots show padding. Using b"." makes the fill visible; a real display would use spaces.
Extra byte on the right. READY and ERR leave an odd number of spare bytes; in this even width the extra one goes to the right.
Example 3 — The Fill Must Be Exactly One Byte
Try a two-byte fill and a str fill, then pad with zero bytes.
python
word = b"ok"
try:
word.center(6, b"=-")
except TypeError:
print("fillbyte must be exactly one byte")
try:
word.center(6, "=")
except TypeError:
print("fillbyte cannot be a str")
print(word.center(6, b"\x00"))
Output
fillbyte must be exactly one byte
fillbyte cannot be a str
b'\x00\x00ok\x00\x00'
How It Works
Two bytes rejected. A multi-byte fill raises TypeError, even though it is bytes.
Text rejected. A one-character str is still the wrong type.
Any single byte works. The zero byte pads both sides and appears as \x00 in the repr.
Frequently Asked Questions
An ASCII space, b" ".
No. It must be bytes or bytearray of length 1; otherwise TypeError is raised.
It depends on the lengths involved, so do not rely on it. Use ljust() or rjust() if you need exact control.
No. If width is not larger than the data, the data is returned unchanged.
In bytes. Multi-byte UTF-8 characters count as several bytes.