Printing a degree symbol in Python
While trying to show the ball angle in PyGame I fired up charmap in Windows, copied the degree symbol, and pasted it into my code. As expected, I got this error in Komodo while trying print:
DeprecationWarning: Non-ASCII character ‘\xb0′ in file Python-1.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
execfile(file, globals, locals)
After taking a look at PEP 0263 and a python-tutor post, I was able to figure out how to get it to print without the warning. Here’s what you need to do:
degree_symbol = unichr(176).encode("latin-1")
print degree_symbol
That should properly display the symbol (i.e. °). Here it is rendered as text in PyGame:
Pretty simple if you are familiar with Unicode, but good to know.