Converting bytes to str in Python 3.0 via Functions
Thursday February 26, 2009
Last week, we looked at how to convert between strings of the
str data type and strings of the bytes data type using methods in the string class of Python 3.x. Another way that is a bit more straightforward is to simply call the function form of the target data type. So, to convert to str, call
str(<bytes string>, <encoding>)
Similarly, you can make bytes from str with
bytes(<str string>, <encoding>)
For more on the syntax of each, see "Converting str to bytes via Functions" in "A Guide to Text vs Data in Python 3.0".