Python bytes.center() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

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)
CallWhat 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

ParameterDescription
widthThe total length of the result in bytes.
fillbyteOptional. A single byte used for padding. Default b" ".

Return Value

Return ValueDescription
bytesThe 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))

How It Works

  1. Even padding. Six spare bytes are split three and three.
  2. Custom fill. b"*" replaces the spaces.
  3. Odd padding. Five spare bytes cannot be split evenly; with this width the extra byte goes on the left.
  4. 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))

How It Works

  1. Constant width. Every line is exactly 16 bytes, whatever the message length.
  2. Dots show padding. Using b"." makes the fill visible; a real display would use spaces.
  3. 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"))

How It Works

  1. Two bytes rejected. A multi-byte fill raises TypeError, even though it is bytes.
  2. Text rejected. A one-character str is still the wrong type.
  3. 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.

Next: bytes.count()

Count non-overlapping occurrences of a byte sequence or a single byte value.

count() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful