Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
So far we’ve focused on str and
bytes, since they subsume Python
2’s unicode and str. Python 3.0 has a third string type,
though—bytearray, a mutable
sequence of integers in the range 0 through 255, is essentially a
mutable variant of bytes. As such,
it supports the same string methods and sequence operations as
bytes, as well as many of the
mutable in-place-change operations supported by lists. The bytearray type is also available in Python
2.6 as a back-port from 3.0, but it does not enforce the strict
text/binary distinction there that it does in 3.0.
Let’s take a quick tour. bytearray objects may be created by calling
the bytearray built-in. In Python
2.6, any string may be used to initialize: