🌐
GeeksforGeeks
geeksforgeeks.org › python › struct-module-python
struct module in Python - GeeksforGeeks
May 10, 2025 - The struct module in Python allows you to work with binary data by providing functionality to convert between Python values and C-style binary data. This is particularly useful when dealing with binary file formats or network protocols.
🌐
Python
docs.python.org › 3 › library › struct.html
struct — Interpret bytes as packed binary data
Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...
🌐
W3Schools
w3schools.com › Python › ref_module_struct.asp
Python struct Module
The struct module converts between Python values and C structs represented as Python bytes objects.
🌐
AskPython
askpython.com › python-modules › python-struct-module
Python struct Module - AskPython
January 21, 2020 - The Python struct module is used to provide a simple Pythonic interface to access and manipulate C’s structure datatype.
🌐
Jython
jython.org › jython-old-sites › docs › library › struct.html
7.3. struct — Interpret strings as packed binary data — Jython v2.5.2 documentation
This module performs conversions between Python values and C structs represented as Python strings. It uses format strings (explained below) as compact descriptions of the lay-out of the C structs and the intended conversion to/from Python values.
🌐
Educative
educative.io › answers › what-is-the-python-struct-module
What is the Python struct module?
The struct module in Python is used to convert native Python data types such as strings and numbers into a string of bytes and vice versa.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to use python struct functions
How to Use Python Struct Functions | phoenixNAP KB
December 16, 2025 - The Python struct module offers several functions to pack and unpack structs and an error catcher for exceptions.
🌐
Python Module of the Week
pymotw.com › 2 › struct
struct – Working with Binary Data - Python Module of the Week
Now available for Python 3! Buy the book! ... The struct module includes functions for converting between strings of bytes and native Python data types such as numbers and strings.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › struct in python
Struct in Python| Scaler Topics
December 12, 2022 - As discussed earlier, struct in ... our industry-leading programs designed for career success ... The module refers to the module used for conversion among C structs and Python values....
🌐
Pythoncomplexity
pythoncomplexity.com › stdlib › struct
Struct - Python Big-O: Time & Space Complexity
The struct module converts between Python values and packed binary records described by a format string such as '<4sIh'. The work is C, done in one pass over a compiled list of field codes: packing writes one record into a bytes object or a buffer you supply, unpacking reads one record into a tuple.
🌐
TutorialsPoint
tutorialspoint.com › article › struct-module-in-python
struct module in Python
March 25, 2026 - The struct module in Python converts native Python data types into strings of bytes and vice versa. It's a built-in module that uses C-style format characters to specify data types and their binary representation.
🌐
CodersLegacy
coderslegacy.com › home › python › python struct module – pack, unpack binary data
Python Struct Module - Pack, Unpack Binary Data - CodersLegacy
April 21, 2022 - The struct module in Python was introduced with the Python 3.x version. The struct module is used to convert native Python data types such as strings and numbers into a string of bytes and vice versa.
🌐
Reddit
reddit.com › r/programminglanguages › statically-typed equivalent of python's `struct` module?
r/ProgrammingLanguages on Reddit: Statically-typed equivalent of Python's `struct` module?
March 13, 2025 -

In the past, I've used Python's struct module as an example when asked if there are any benefits of dynamic typing. It provides functions to convert between sequences of bytes and Python values, controlled by a compact "format string". Lua also supports very similar conversions via the string.pack & unpack functions.

For example, these few lines of Python are all it takes to interpret the header of a BMP image file and output the image's dimensions. Of course for this particular example it's easier to use an image library, but this code is much more flexible - it can be changed to support custom file types, and iteratively modified to investigate files of unknown type:

file_name = input('File name: ')
with open(file_name, 'rb') as f:
    signature, _, _, header_size, width, height = struct.unpack_from('<2sI4xIIii', f.read())
assert signature == b'BM' and header_size == 40
print(f'Dimensions: {width}x{abs(height)}')

Are there statically-typed languages that can offer similarly concise code for binary manipulation? I can see a couple of ways it could work:

  • Require the format string to be a compile-time constant. The above call to unpack_from could then return Tuple<String, Int, Int, Int, Int, Int>

  • Allow fully general format strings, but return List<Object> and require the programmer to cast the Objects to the correct type:

      assert (signature as String) == 'BM' and (header_size as Int) == 40
      print(f'Dimensions: {width as Int}x{abs(height as Int)}')

Is it possible for a statically-typed language to support a function like struct.unpack_from? The ones I'm familiar with require much more verbose code (e.g. defining a dataclass for the header layout). Or is there a reason that it's not possible?

🌐
Python Module of the Week
pymotw.com › 3 › struct
struct — Binary Data Structures
December 9, 2018 - The struct module includes functions for converting between strings of bytes and native Python data types such as numbers and strings.
🌐
MIT
web.mit.edu › people › amliu › vrut › python › lib › module-struct.html
4.5 Built-in Module struct
This module performs conversions between Python values and C structs represented as Python strings.
🌐
Sunysb
insti.physics.sunysb.edu › itp › computing › doc › python › python-texinfo › struct.html
struct -- Python library reference
This module performs conversions between Python values and C structs represented as Python strings.
🌐
Coderancher
coderancher.us › 2024 › 01 › 26 › structs-python-c-tutorial
Understanding and Leveraging Structs in Python with the struct Module – www.CodeRancher.Us
What is a Struct? At its core, a struct is a composite data type that groups variables of different data types under a single name. The struct module in Python extends this concept by providing functionalities for efficiently packing and unpacking binary data.
🌐
Parseltongue
parseltongue.co.in › understanding-the-struct-module-in-python
ParselTongue - Understanding the struct Module in Python
September 6, 2024 - The struct module in Python allows you to pack and unpack data into/from binary formats using format strings.
🌐
USNA
usna.edu › Users › cs › choi › it430 › lec › l03 › lec.html
[IT430] Class 3: Struct Packing, Type Conversion
The module struct performs conversions between Python values and C structs represented as Python bytes objects.
🌐
GitHub
github.com › python › cpython › blob › main › Modules › _struct.c
cpython/Modules/_struct.c at main · python/cpython
/* struct module -- pack values into and (out of) bytes objects */ · /* New version supporting byte order, alignment and size options, byte strings, and unsigned numbers */ · #ifndef Py_BUILD_CORE_BUILTIN · # define Py_BUILD_CORE_MODULE 1 · #endif · · #include "Python.h" #include "pycore_bytesobject.h" // _PyBytesWriter ·
Author: python