IordanouGiannis
IordanouGiannis

Reputation: 4357

Why a string is displayed differently inside a list and outside in python?

I am scraping a web page and I get what I guess is a utf-8 text.So, the following happens:

# -*- coding: utf-8 -*-
import urllib2
import json

url = '....'
data = json.load(urllib2.urlopen(url))
a = u'\u0391\u0391\u039b\u0395\u039d'
print a
ΑΑΛΕΝ
print [a]
[u'\u0391\u0391\u039b\u0395\u039d']

Any ideas why the second time is not displayed as the first ?

Upvotes: 2

Views: 34

Answers (1)

mgilson
mgilson

Reputation: 309891

print calls str implicitly on the arguments, but str for a list call repr on all of the inner elements. e.g.:

>>> class Foo(object):
...     def __str__(self):
...         return 'world'
...     def __repr__(self):
...         return 'hello'
... 
>>> f = Foo()
>>> print repr(f)
hello
>>> print f
world
>>> print [f]
[hello]

Upvotes: 3

Related Questions