:\codec:\python \python from struct import pack pack('> sh' 'spam' '\ \ \ \ spam\ \ str in ( -bit stringssince bytes has an almost identical interface to that of str in and xthoughmost programmers probably won' need to care--the change is irrelevant to most existing codeespecially since reading from binary file creates bytes automatically although the last test in the following example fails on type mismatchmost scripts will read binary data from filenot create it as string as we do herec:\codec:\python \python import struct struct pack('> sh' 'spam' '\ \ \ \ spam\ \ vals struct unpack('> sh'bvals ( 'spam' vals struct unpack('> sh' decode()typeerror'strdoes not support the buffer interface apart from the new syntax for bytescreating and reading binary files works almost the same in as it does in stillcode like this is one of the main places where programmers will notice the bytes object typec:\codec:\python \python write values to packed binary file open('data bin''wb'import struct data struct pack('> sh' 'spam' data '\ \ \ \ spam\ \ write(data close(read values from packed binary file open('data bin''rb'data read(data '\ \ \ \ spam\ \ values struct unpack('> sh'datavalues ( 'spam' open binary output file create packed binary data bytes in xnot str write to the file open binary input file read bytes extract packed binary data back to python objects once you've extracted packed binary data into python objects like thisyou can dig even further into the binary world if you have to--strings can be indexed and sliced to get individual bytesvaluesindividual bits can be extracted from integers with bitwise operatorsand so on (see earlier in this book for more on the operations applied here) unicode and byte strings