the struct module the struct module provides struct pack()struct unpack()and some other functionsand the struct struct(class the struct pack(function takes struct format string and one or more values and returns bytes object that holds all the values represented in accordance with the format the struct unpack(function takes format and bytes or bytearray object and returns tuple of the values that were originally packed using the format for exampledata struct pack("< " - items struct unpack("< "datadata = '\ \ \xf \xffitems =( - format strings consist of one or more characters most characters represent value of particular type if we need more than one value of type we can either write the character as many times as there are values of the type ("hh")or precede the character with count as we have done here (" "many format characters are described in the struct module' online documentationincluding " ( -bit signed integer)" ( -bit unsigned integer)" ( -bit signed integer--used in the examples here)" ( -bit unsigned integer)" ( -bit signed integer)" ( -bit unsigned integer)" ( -bit signed integer)" ( -bit unsigned integer)" ( -bit float)" ( -bit float--this corresponds to python' float type)"?(boolean)" (bytes or bytearray object--byte strings)and many others for some data types such as multibyte integersthe processor' endianness makes difference to the byte order we can force particular byte order to be used regardless of the processor architecture by starting the format string with an endianness character in this book we always use "<"which means little-endian since that' the native endianness for the widely used intel and amd processors big-endian (also called network byte orderis signified by ">(or by "!"if no endianness is specified the machine' endianness is used we recommend always specifying the endianness even if it is the same as the machine being used since doing so keeps the data portable the struct calcsize(function takes format and returns how many bytes struct using the format will occupy format can also be stored by creating struct struct(object giving it the format as its argumentwith the size of the struct struct(object given by its size attribute for exampletwo_shorts struct struct("< "data two_shorts pack( - items two_shorts unpack(datadata = '\ \ \xf \xffitems =( - in both examples is bbut this is transformed into the bytes because we have used little-endian byte ordering