How can I get a °
(degree) character into a string?

- 50,140
- 28
- 121
- 140

- 15,152
- 31
- 85
- 111
-
1Please see [this answer](http://stackoverflow.com/a/37678518/4099593) if you are attempting to print the degree sign on a ***Windows*** system. – Bhargav Rao Jun 07 '16 at 12:32
8 Answers
This is the most coder-friendly version of specifying a Unicode character:
degree_sign = u'\N{DEGREE SIGN}'
Escape Sequence: \N{name}
Meaning: Character named name in the Unicode database
Reference: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
Note:
"N" must be uppercase in the
\N
construct to avoid confusion with the\n
newline characterThe character name inside the curly braces can be any case
It's easier to remember the name of a character than its Unicode index. It's also more readable, ergo debugging-friendly. The character substitution happens at compile time, i.e. the .py[co]
file will contain a constant for u'°'
:
>>> import dis
>>> c= compile('u"\N{DEGREE SIGN}"', '', 'eval')
>>> dis.dis(c)
1 0 LOAD_CONST 0 (u'\xb0')
3 RETURN_VALUE
>>> c.co_consts
(u'\xb0',)
>>> c= compile('u"\N{DEGREE SIGN}-\N{EMPTY SET}"', '', 'eval')
>>> c.co_consts
(u'\xb0-\u2205',)
>>> print c.co_consts[0]
°-∅

- 3,777
- 9
- 27
- 53

- 92,761
- 29
- 141
- 204
>>> u"\u00b0"
u'\xb0'
>>> print _
°
BTW, all I did was search "unicode degree" on Google. This brings up two results: "Degree sign U+00B0" and "Degree Celsius U+2103", which are actually different:
>>> u"\u2103"
u'\u2103'
>>> print _
℃

- 45,184
- 7
- 50
- 57
-
4
-
@SilentGhost: Well yeah, but I didn't remember the numpad code for ° and didn't feel like looking it up at the time. – JAB Jul 09 '10 at 18:30
-
1Who remembers numpad codes? :) [Compose key](https://en.wikipedia.org/wiki/Compose_key) sequences are a _lot_ easier to remember; degree is just Compose-o-o. Compose key is standard on X windows systems, but it's also available for Microsoft Windows; see the Wikipedia link. – PM 2Ring Jun 07 '16 at 12:55
Put this line at the top of your source
# -*- coding: utf-8 -*-
If your editor uses a different encoding, substitute for utf-8
Then you can include utf-8 characters directly in the source

- 295,403
- 53
- 369
- 502
-
1Assuming your editor does UTF-8. If your editor uses a different charset then indicate that instead. – Ignacio Vazquez-Abrams Jul 09 '10 at 19:19
just use \xb0 (in a string);
python will convert it automatically

- 59,234
- 49
- 233
- 358

- 121
- 1
- 4
Above answers assume that UTF8 encoding can safely be used - this one is specifically targetted for Windows.
The Windows console normaly uses CP850 encoding and not utf-8, so if you try to use a source file utf8-encoded, you get those 2 (incorrect) characters ┬░
instead of a degree °
.
Demonstration (using python 2.7 in a windows console):
deg = u'\xb0` # utf code for degree
print deg.encode('utf8')
effectively outputs ┬░
.
Fix: just force the correct encoding (or better use unicode):
local_encoding = 'cp850' # adapt for other encodings
deg = u'\xb0'.encode(local_encoding)
print deg
or if you use a source file that explicitely defines an encoding:
# -*- coding: utf-8 -*-
local_encoding = 'cp850' # adapt for other encodings
print " The current temperature in the country/city you've entered is " + temp_in_county_or_city + "°C.".decode('utf8').encode(local_encoding)

- 143,923
- 11
- 122
- 252
Using python f-string
, f"{var}"
, you can use:
theta = 45
print(f"Theta {theta}\N{DEGREE SIGN}.")
Output: Theta 45°.
*improving tzot answer

- 1,512
- 12
- 22
Special case: Sending a string to a 16x2 or 20x4 LCD (e.g. attached to a Raspberry Pi with I2C LCD display):
f"Outside Temp: {my_temp_variable}{chr(223)}"

- 245
- 3
- 9