Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
structure
struct

What is the Python struct module?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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. What this means is that users can parse binary files of data stored in C structs in Python.

It is used mostly for handling binary data stored in files or from network connections, among other sources.

The module is only available in Python 3.x and needs to be imported first by writing

import struct

This process needs to be done at the start of the program.

Struct Functions

There are several functions built into the struct module. Some important ones are:

1. struct.pack()

struct.pack() is the function that converts a given list of values into their corresponding string representation. It requires the user to specify the format and order of the values that need to be converted.

svg viewer

The following code shows how to pack some given data into its binary form using the module’s struct.pack() function.

import struct

packed = struct.pack('i 4s f', 10, b'John', 2500)
print(packed)

The first argument of the function represents the format string. A *format strings specifies the expected layout when packing and unpacking data. The rest of the arguments represent the data that needs to be packed.

These format strings are made up of format characters. Some common ones are:

svg viewer

2. struct.unpack()

This function converts the strings of binary representations to their original form according to the specified format. The return type of struct.unpack() is always a tuple.

svg viewer
import struct

packed = b'\n\x00\x00\x00John\x00@\x1cE'
unpacked = struct.unpack('i 4s f', packed)
print(unpacked)

The function is given a format string and the binary form of data. This function is used to parse the binary form of data stored as a C structure.

3. struct.calcsize()

This function calculates the size of the String representation of struct with a given format string.

svg viewer
import struct

size = struct.calcsize('i 4s f')
print("Size in bytes: {}".format(size))

RELATED TAGS

python
structure
struct
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring