Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying the Degree Symbol in Python

Tags:

python

I've got the following code that displays the degree symbol and it works fine with Python3 in PyCharm:

print(u'\u00b0'+ " F")

But when I move the code over to Python3 on my Pi I get the following error:

print(u'\u00b0'+ " F")
              ^ SyntaxError: invalid syntax

Does anyone have any idea on why this happens and how to fix it?

like image 619
user3723727 Avatar asked Oct 19 '25 06:10

user3723727


1 Answers

In Python versions 3.0 through 3.2, the u prefix on a string literal was not allowed. Python 3.3 reintroduced it to aid in writing code that works in both Python 2 and Python 3 (see PEP 414).

I suspect your code is failing in one of the older versions of Python 3, and working on the other system in a newer version. In any version of Python 3, the u is unnecessary. You can just write '\u00b0'+ " F" or even '\u00b0 F' instead.

like image 78
Blckknght Avatar answered Oct 21 '25 20:10

Blckknght