预计阅读本页时间:-
Using 3.0 Bytes Objects
We studied a wide variety of operations available for Python 3.0’s general str string type in Chapter 7; the basic string type works identically in 2.6 and 3.0, so we won’t rehash this topic. Instead, let’s dig a bit deeper into the operation sets provided by the new bytes type in 3.0.
As mentioned previously, the 3.0 bytes object is a sequence of small integers, each of which is in the range 0 through 255, that happens to print as ASCII characters when displayed. It supports sequence operations and most of the same methods available on str objects (and present in 2.X’s str type). However, bytes does not support the format method or the % formatting expression, and you cannot mix and match bytes and str type objects without explicit conversions—you generally will use all str type objects and text files for text data, and all bytes type objects and binary files for binary data.