Print Degree Symbol in Python

How to print degree symbol in Python?

In Python, we use the print() function to display some characters. We can not only print the characters visible on our keyboard but can also display the characters that belong to the Unicode character set.

In this article, we will learn how to print degree symbol in Python. For this, we will display the character from the Unicode database using several methods.

Using the \N escape sequence to print degree symbol in Python

In programming, we often use escape sequences that can represent some other special or illegal characters when translated into a string. Some common escape sequences are \n, \t, and many more.

We can use escape the escape sequence \N{char_name} to display some character from the Unicode characters set. It will display the character that matches the corresponding char_name in the sequence.

Before using this sequence in a string, we need to specify the prefix u. This indicates to the Python interpreter that it is dealing with a Unicode string. However, this may not be required in Python 3 since it supports the Unicode strings in the string class.

We will use this sequence to print degree symbol in Python.

Output:

8°

In the above example, we print the degree symbol using the \N{DEGREE_SIGN} sequence. The DEGREE_SIGN matches the degree symbol from the Unicode database.

Using the \u escape sequence to print degree symbol in Python

We can also use the \u escape sequence to print degree symbol in Python. It displays the Unicode character based on the 16-bit hexadecimal value. We need to specify the hexadecimal code for the degree symbol to print it.

See the code below.

Output:

8°

Using the \xb escape sequence to print degree symbol in Python

We can also specify byte strings to represent Unicode characters. This can be done using the \xb sequence. To print degree symbol in Python, we will use the same hexadecimal code with this sequence.

For example,

Output:

8°

Using the chr() function to print degree symbol in Python

We can use the chr() function in Python to display Unicode strings using their numeric representation.

The numeric point for the degree symbol is 176 and can be used to print degree symbol in Python.

Note that this method is available in recent versions of Python 3.6 only.

See the code below.

Output:

8 °

Conclusion

To conclude, we discussed how to print degree symbol in Python. We displayed the character from the Unicode dataset.

The first few methods involved the use of escape sequences. The \N sequence can print degree symbol in Python using the symbol name and the \u and \xb sequence displays it using the 16-bit hexadecimal code.

We can also use the chr() function for the same which was introduced in recent versions of Python. It displays the character using the numeric point in the Unicode database.

That’s all about how to print degree symbol in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *