Python打印特殊字符

时间:2013-08-10 14:21:10

标签: python character

在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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

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

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

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

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

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

它会打印出来:

√
∞
²
³
≤
≥
±
≠

另请参阅SyntaxError of Non-ASCII character

希望有所帮助。