On this page
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
- Prefix guide:
!/>= big-endian (network),<= little-endian,== native without padding.
REPL Inspect
from struct import pack
help(pack)
Linked from
- tuts/python3/stdlib_module/struct
- tuts/python3/stdlib_module/struct/calcsize
- tuts/python3/stdlib_module/struct/error
- tuts/python3/stdlib_module/struct/iter_unpack
- tuts/python3/stdlib_module/struct/pack_into
- tuts/python3/stdlib_module/struct/Struct
- tuts/python3/stdlib_module/struct/unpack
- tuts/python3/stdlib_module/struct/unpack_from
Related notes
- tuts/python3/stdlib_module/struct/calcsize Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/error Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/iter_unpack Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/pack_into Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/Struct Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/unpack links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/unpack_from links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib