I have a list compiled from excel cells, using python - say listlist
. Each element in the cell/list is in unicode. When I print the list as
pri
when you are printing a list, python prints a representation of the list.
the default representation of a list is to print an opening square brackets ([
), then the representation of each element separated by a comma (,
), the a closing square bracket (]
). (to be precise the representation of an object is what is returned when calling its __repr__()
member). note that the default representation of a unicode string is u'...'
.
now, when you are printing a string, you are not printing the representation of the string, you are printing the string (the value returned by calling the __str__()
member). and the string does not include formatting charaters like unicode flags, quotes, etc...
There are two ways to turn a Python value into a string str
and repr
(and their equivalent class methods __str__
and __repr__
). The difference comes from these two functions. From the Python tutorial:
The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax).
The print
statement calls str
on the object you pass in, but the list's __str__
method calls repr
on it's values. The values of your list are unicode strings. In human readable form the fact it's unicode is irrelevent, so you don't get a u
, or even quote marks. In computer readable form it is important, so the u
is included.
When printing the list, it shows each object within the list using the object's __repr__
When printing the object alone, it uses the object's __str__
This is because print list
is equivalent to
print "[", ", ".join(repr(i) for i in list), "]"
repr(s)
is u"blabla"
for a unicode string while print s
prints only blabla
.