How to Print Degree Symbol in Python

Preet Sanghavi Feb 02, 2024
  1. Use the chr Function to Print the Degree Symbol in Python
  2. Use f-string to Print the Degree Symbol in Python
  3. Use Unicode to Print the Degree Symbol in Python
  4. Use the ord Function to Print the Degree Symbol in Python
  5. Conclusion
How to Print Degree Symbol in Python

The degree (°) symbol is a vital indicator of temperature. In Python, printing the degree symbol is a common requirement for tasks related to temperature, angles, or any other context where degrees are used.

There are several methods to achieve this, ranging from using Unicode characters to incorporating external libraries. This topic explores various approaches to print the degree symbol in Python, providing users with the flexibility to choose the method that best fits their specific needs and preferences.

Use the chr Function to Print the Degree Symbol in Python

We can use the chr function in Python to print the degree symbol. Using the chr function to print the degree symbol involves obtaining the character representation of the Unicode code point for the degree symbol.

The Unicode code point for the degree symbol is 176, so you can use chr(176) to obtain the character corresponding to that code point. We can use the following code block to print the degree symbol in Python.

Example Code:

print(chr(176))

Output:

°

We can also write it along with the temperature. This operation can be performed with the help of the following block of code in Python.

Example Code:

print("23", chr(176), sep="")

Output:

23°

In this code, the chr(176) function is employed to obtain the character corresponding to the Unicode code point 176, which represents the degree symbol. By setting sep="", we specify an empty string as the separator between the elements we’re printing.

Therefore, the output of this code will be 23°, where the degree (°) symbol is seamlessly appended to the number 23. Note that the sep function indicates the space between the symbol and the temperature value.

Use f-string to Print the Degree Symbol in Python

The f-string is yet another format in Python that helps us present strings and characters in a particular manner.

Using f-string to print the degree symbol in Python involves incorporating the Unicode character for the degree symbol directly into a formatted string. The Unicode character for the degree symbol is represented by \N{DEGREE SIGN}.

Example Code:

Temp = 22

print(f"Temperature {Temp}\N{DEGREE SIGN}")

Output:

Temperature 22°

In this code, we have a variable Temp assigned the value 22. We use an f-string with the print function to display a message that includes the temperature value and the degree symbol.

The Unicode character for the degree sign, represented as \N{DEGREE SIGN}, is included within the f-string. The output of this code will be Temperature 22°, where the degree symbol is added after the temperature value.

Use Unicode to Print the Degree Symbol in Python

Using Unicode to print the degree symbol in Python involves creating a string that contains the Unicode character for the degree symbol. In Python, the Unicode character for the degree symbol is '\u00b0'.

Example Code:

degree_symbol = "\u00b0"
print(f"45{degree_symbol}")

Output:

45°

In this code, we have a variable degree_symbol assigned the Unicode representation of the degree symbol, which is '\u00b0'. The print statement uses an f-string to display the string 45 followed by the degree symbol.

When executed, the output will be 45°, where the degree symbol is appended to the number 45.

Use the ord Function to Print the Degree Symbol in Python

The ord() function in Python is used to get the Unicode code point of a specified character. Using the ord function to print the degree symbol in Python involves finding the Unicode code point of the degree ('°') symbol and then converting it to a string using the chr function.

Example Code:

degree_symbol = str(chr(ord("°")))
print(f"48{degree_symbol}")

Output:

48°

In this code, we have a variable degree_symbol assigned the string representation of the degree symbol. The ord('°') function is used to get the Unicode code point of the degree symbol, then chr converts it back to a character, and finally, str converts it to a string.

The print statement uses an f-string to display the string 48 followed by the degree symbol. When executed, the output will be 48°, where the degree symbol is appended to the number 48.

Conclusion

This tutorial has explored various methods for printing the degree symbol in Python. We learned how to use the chr function to obtain the degree symbol’s Unicode character and incorporate it into our output.

Additionally, the f-string was introduced as a powerful tool for formatting strings and including the degree symbol seamlessly in temperature displays. The tutorial also covered the use of Unicode directly and the ord function for obtaining the degree symbol.

With these diverse approaches, Python developers can choose the method that best suits their coding preferences and specific application requirements.

Preet Sanghavi avatar Preet Sanghavi avatar

Preet writes his thoughts about programming in a simplified manner to help others learn better. With thorough research, his articles offer descriptive and easy to understand solutions.

LinkedIn GitHub

Related Article - Python Symbol