【问题标题】:Python printing special Characters [duplicate]Python打印特殊字符[重复]
【发布时间】:2013-08-12 08:03:42
【问题描述】:

在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

我该如何解决这个问题?

【问题讨论】:

  • 抱歉没有回答我的问题
  • # code: utf-8放在源代码的第一行。

标签: python character


【解决方案1】:

运行此代码会生成您提供的相同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

希望对您有所帮助。

【讨论】:

  • 太棒了!谢谢,答案很清楚了
猜你喜欢
  • 2014-07-27
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
相关资源
最近更新 更多