How to get ° character in a string in python?

PythonString

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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 character

  • The 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]
°-∅

Solution 2 - Python

>>> 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 _
℃

Solution 3 - Python

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

Solution 4 - Python

You can also use chr(176) to print the degree sign. Here is an example using python 3.6.5 interactive shell:

https://i.stack.imgur.com/spoWL.png

Solution 5 - Python

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)

Solution 6 - Python

just use \xb0 (in a string); python will convert it automatically

Solution 7 - Python

Using python f-string, f"{var}", you can use:

theta = 45
print(f"Theta {theta}\N{DEGREE SIGN}.")

Output: Theta 45°.

*improving tzot answer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichardView Question on Stackoverflow
Solution 1 - PythontzotView Answer on Stackoverflow
Solution 2 - PythonadamkView Answer on Stackoverflow
Solution 3 - PythonJohn La RooyView Answer on Stackoverflow
Solution 4 - PythonthepraskView Answer on Stackoverflow
Solution 5 - PythonSerge BallestaView Answer on Stackoverflow
Solution 6 - PythonJustinView Answer on Stackoverflow
Solution 7 - PythonMuhammad YasirroniView Answer on Stackoverflow