← All notes

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

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

Sources

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

Permanent ID: iil:note:bk4b60fb

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.},
}

Markdown

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.

All artifact files (JSON index)

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.