My code looks like :
# -*- coding: utf-8 -*-
print ["asdf", "中文"]
print ["中文"]
print "中文"
The output in the Eclipse console is very strange:
['asdf', '\xe4\xb8\xad\xe6\x96\x87']
['\xe4\xb8\xad\xe6\x96\x87']
中文
My first question is: why did the last line get the correct output, and the others didn't?
And my second question is: how do I correct the wrong ones (to make them output real characters instead of the code that begins with "x") ?
Thank you guys!!
When you
print foo
, what gets printed out isstr(foo)
.However, if
foo
is alist
,str(foo)
usesrepr(bar)
for each elementbar
, notstr(bar)
.The
str
of a string is the string itself; therepr
of a string is the string inside quotes, and escaped.If you want to print the
str
of every element in alist
, you have to do that explicitly. For example:There have been sporadic proposals to change this behavior, so
str
on a sequence callsstr
on its members. PEP 3140 is the rejected proposal. This thread from 2009 explains the design rationale behind rejecting it.But primarily, it's either so these don't print the same thing:
Or, paraphrasing Ned Batchelder:
repr
is always for geeks;str
is for humans when possible, but printing lists with their brackets and commas is already for geeks.The first two are using the
__repr__
of the strings, the last one is using the__str__
methodYou could use