Python struct: pack and unpack binary data
Answer: struct.pack(format, v1, v2, ...) returns a bytes object packed according to the format string, and struct.unpack(format, buffer) reverses it. Start the format string with <, > or ! to fix the byte order and use standard sizes; this module converts between Python values and C structs represented as bytes objects.
Examples
import struct
struct.pack('>h', 1023) # b'\x03\xff' (big-endian)
struct.pack('<h', 1023) # b'\xff\x03' (little-endian)
header = struct.pack('<4sIH', b'DATA', 512, 7)
magic, size, version = struct.unpack('<4sIH', header)
struct.calcsize('<4sIH') # 10
record = struct.Struct('<IH') # compile once, reuse
buf = bytearray(record.size * 2)
record.pack_into(buf, 0, 1, 2)
for item in struct.iter_unpack('<IH', bytes(buf)):
print(item)
Byte order prefixes
| Prefix | Byte order | Size and alignment | |---|---|---| | @ (default) | native | native size and native alignment | | = | native | standard size, no alignment | | < | little-endian | standard size, no alignment | | > | big-endian | standard size, no alignment | | ! | network (big-endian) | standard size, no alignment |
If the first character is not one of these, @ is assumed.
Functions
pack()arguments must match the values required by the format exactly.unpack()always returns a tuple, even for exactly one item, and the buffer size must matchcalcsize(fmt).pack_into()writes packed bytes into a writable buffer at offset, andunpack_from()reads from an offset; a negative offset counts from the end of buffer.iter_unpack()yields one tuple per equally sized chunk; the buffer size must be a multiple of the format size.
Struct objects
Creating a Struct once and calling its methods is more efficient than the module-level functions, because the format string is compiled only once.
See also
Claims
- id: C1 claim: unpack returns a tuple even for one item. source: S1 evidence: "The result is a tuple even if it contains exactly one item."
- id: C2 claim: Without a prefix character, @ is assumed. source: S1 evidence: "If the first character is not one of these, '@' is assumed."
- id: C3 claim: Struct objects compile the format string once. source: S2 evidence: "the format string is only compiled once."
Sources
- [S1] struct - Interpret bytes as packed binary data. Python docs. https://docs.python.org/3/library/struct.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Doc/library/struct.rst. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Doc/library/struct.rst . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, struct - Interpret bytes as packed binary data; python/cpython (GitHub), Doc/library/struct.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, struct - Interpret bytes as packed binary data; python/cpython (GitHub), Doc/library/struct.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python struct: pack and unpack binary data." iinventedlight Library, iil:note:bk4b60fb, version of 2026-11-18. https://iinventedlight.com/library/notes/python-struct/. License: CC-BY-4.0.
@misc{iil_note_bk4b60fb,
author = {{iinventedlight}},
title = {{Python struct: pack and unpack binary data}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-struct/},
note = {Permanent ID iil:note:bk4b60fb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-struct.table.json (table; CC-BY-4.0)
- python-struct.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-struct.md.