开发者

How do I print the root sign in Python?

I tried this:

print u"\u221A"

But it won't work in Python.

Can someone say me 开发者_JAVA技巧how to do this in python? I need it to show what I have done.


print u"\u221A".encode('utf-8')

Assuming by "won't work", you mean you're getting the UnicodeEncodeError I get running your code in interactive mode. Your terminal's encoding may vary.


Check your LANG environment variable as well:

% 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)


Is the following the desired behavior?

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"
√

Without knowing the error message you're getting, some things to check would be whether or not you can print other unicode characters using the method you have in your terminal. If not, your problem is that you're probably not configured for unicode.


You really should have explained what happened when you tried it. This is what happened when I tried it on Python 2.7 using Ubuntu's Gnome terminal program.


I was unable to print it to a document on Python 3.5 (I just used a for loop that when it found a character [\u221a] it appended it to a second result variable [in the form of a previously empty list] then printed the first to the console and the second [after joining it to a string of course] to the document), but when I printed it to a console, it worked just fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜