0

In Python how do I print special characters such as √, ∞, ²,³, ≤, ≥, ±, ≠

When I try printing this to the console I the get this error:

print("√")

SyntaxError: Non-ASCII character '\xe2' in file /Users/williamfiset/Desktop/MathAid - Python/test.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How do I get around this?

will.fiset
  • 1,524
  • 1
  • 19
  • 29

1 Answers1

2

Running this code results into the same SyntaxError you've provided:

chars = ["√", "∞", "²","³", "≤", "≥", "±", "≠"]
for c in chars:
    print(c)

But if I add # -*- coding: utf-8 -*- at the top of the script:

# -*- coding: utf-8 -*-

chars = ["√", "∞", "²","³", "≤", "≥", "±", "≠"]
for c in chars:
    print(c)

it will print:

√
∞
²
³
≤
≥
±
≠

Also, see SyntaxError of Non-ASCII character.

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195