The encode method is one of Python's built-in functions for string handling. It is not part of the string module. The basic format of the program call is as follows:
The quotation marks around the encoding are required. In this case, string y has already been assigned and is now being assigned to x in an encoding. The encoding being used may be one of a myriad of encodings that Python can use; for the complete list, see the Python documentation.
x = y.encode("<encoding>")
In the case of 8-bit Unicode, we want to assign the value of body to output using a "utf-8" encoding. So, rewrite line 105 of the program to read as follows:
Now, when the output is printed in the subsequent line, it will be in Unicode. This also enables the web browser to render it properly.
output = body.encode("utf-8")
Other tutorials in this series:
Part 1 | 2 | 3 | 4 | 5
Get the Code!