31 January 2006 ~ 1 Comment

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:

Degree symbol

Pretty simple if you are familiar with Unicode, but good to know.

One Response to “Printing a degree symbol in Python”

  1. Anand 16 January 2009 at 8:01 am Permalink

    Hey good solution.. especially ver simple..

    Thanks a lot..


Leave a Reply