In UTF-8, the degree sign (Unicode U+00B0) is encoded as C2 B0
. The "Â" letter that got printed is the Unicode U+00C2 character. It looks like Python prints the UTF-8 bytes as desired, but the browser did not interpret it correctly.
When you don't specify what encoding to use in an HTML document, the browser will take the liberty to guess/auto-detect one and use it. In your case, they took the wrong guess (e.g. guessing it as Latin-1) and so the weird character got displayed.
There are some alternative solutions:
- As pointed out by @Tom Karzes in the comment, use the HTML escapes
°
or °
- Specify the encoding of your HTML document. It is a best practice to always do this. There are several different ways to do it, here are some for summary:
a. The meta charset tag
Add the following snippet
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
...
</head>
...
</html>
b. Content-Type
HTTP header
Add the following HTTP header to your HTTP response:
Content-Type: text/html; charset=utf-8
Once the encoding is specified, the same python code should be interpreted correctly by the browser and the undesired character wouldn't be displayed.