On this page
  1. struct.pack
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

struct.pack

Prev: struct.iter_unpack | Up: struct | Next: struct.pack_into
Rel: struct.calcsize, struct.error, struct.iter_unpack, struct.pack_into
Ref: docs.python.org — struct.pack
Tags: #public #python3 #stdlib #struct


Import

import struct
from struct import pack

Signature & Overview

struct.pack(format, v1, v2, ...) -> bytes

Return a bytes object containing the values v1, v2, ... packed according to the format string. See help(struct) for more on format strings.

Usage

import struct

raw = struct.pack(">IH", 0x01020304, 512)
assert raw == b"\x01\x02\x03\x04\x02\x00"

Gotchas

REPL Inspect

from struct import pack
help(pack)

Linked from

Related notes