Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a list using python

Tags:

python

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

print listlist

I see 'u' prepended to each member of the list. But when I print the list like

for member in listlist:
  print member

I do not see the 'u' prepended to the member.

Can someone please explain to me why there is this difference? Is it defined in the xlrd module?

like image 452
Sumod Avatar asked Dec 10 '22 10:12

Sumod


2 Answers

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.

like image 191
filmor Avatar answered Jan 01 '23 22:01

filmor


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__

like image 30
Ioan Alexandru Cucu Avatar answered Jan 01 '23 23:01

Ioan Alexandru Cucu