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:

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:

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:

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?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.