The two data types, str and bytes, are mutually exclusive. One cannot legally combine them into one call. With the distinction between text and data, therefore, comes the need to convert between them.
>>> z = b"Hello World!"To convert from str to bytes, use str.encode().
>>> y = "Hello World!"
>>> type(z)
<class 'bytes'>
>>> type(y)
<class 'str'>
>>> b = str.encode(y)To convert from bytes to str, use bytes.decode().
>>> type(b) <class 'bytes'> >>> b b'Hello World!'
>>> a = bytes.decode(z)For how to accomplish the same thing with Python 3.0's built-in functions, see "Converting str to bytes via Functions", next in this series.
>>> type(a)
<class 'str'>
>>> a
'Hello World!'
Note: Having a better understanding of text and data facilitates good programming. To fill out your understanding of programming under Python 3.0, be sure to check out "Transitioning from 2.x to 3.0".
