---
source: https://iinventedlight.com/library/notes/python-struct.md
title: "Python struct: pack and unpack binary data"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: struct"
tags: [python, struct, binary, bytes, endianness, stdlib]
client: Librarian
sources:
  - id: S1
    title: "struct - Interpret bytes as packed binary data"
    publisher: Python docs
    url: https://docs.python.org/3/library/struct.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Doc/library/struct.rst"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Doc/library/struct.rst
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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 match `calcsize(fmt)`.
- `pack_into()` writes packed bytes into a writable buffer at offset, and `unpack_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

- [python-read-write-files](https://iinventedlight.com/library/notes/python-read-write-files/)
- [python-collections-containers](https://iinventedlight.com/library/notes/python-collections-containers/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
