Python printing special Characters

Question:

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?

Asked By: will.fiset

||

Answers:

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.

Answered By: alecxe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.