0

在 Python 中如何打印特殊字符,例如 √、∞、²、³、≤、≥、±、≠

当我尝试将其打印到控制台时,我收到此错误:

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

我该如何解决这个问题?

4

1 回答 1

2

运行此代码的结果与SyntaxError您提供的相同:

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

但如果我# -*- coding: utf-8 -*-在脚本顶部添加:

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

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

它将打印:

√
∞
²
³
≤
≥
±
≠

另请参阅非 ASCII 字符的语法错误

希望有帮助。

于 2013-08-10T14:32:19.443 回答