如何在Python中打印平方根符号?

0 投票
5 回答
3575 浏览
提问于 2025-04-16 17:31

我试过这个:

print u"\u221A"

但是在Python里不行。

有没有人能告诉我怎么在Python里做到这一点?我需要它来展示我做的事情。

5 个回答

1

以下是你想要的行为吗?

wanderso@araran:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print u"\u221A"
√

在不知道你遇到的错误信息的情况下,有几个地方可以检查一下:看看你在终端里能不能用你现在的方法打印其他的unicode字符。如果不能,那可能是因为你的设置不支持unicode。

2

也要检查一下你的 LANG 环境变量:

% export LANG=en_US.UTF-8
% python -c 'print u"\u221a"'
√

% export LANG=en_US.ASCII     
% python -c 'print u"\u221a"'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u221a' in position 0: ordinal not in range(128)
5
print u"\u221A".encode('utf-8')

假设你所说的“无法工作”是指在交互模式下运行你的代码时出现了Unicode编码错误。那么你的终端编码可能会有所不同。

撰写回答