[Tutor] String encoding
Dave Angel
d at davea.name
Fri Aug 26 19:40:28 CEST 2011
On 08/26/2011 11:49 AM, Prasad, Ramit wrote:
> <snip>
> Yep, it is. Thanks those charts are exactly what I wanted! Now I have another question. What is the difference between what print shows and what the interpreter shows?
>
>>>> print s.decode('latin-1')
> MÉXICO
The decoded characters are a Unicode string. Python prints that string
by encoding it according to whatever sys.stdout is defaulted to. If that
matches your actual terminal, then you see it properly.
>>>> s.decode('latin-1')
> u'M\xc9XICO'
Here, because you don't assign it to anything, the interpreter is
printing a repr() of the object.
>>>> print repr(s)
> 'M\xc9XICO'
Here your code is doing the same thing, but explicitly this time.
>>>> repr(s)
> "'M\\xc9XICO'"
>
>
Here, the repr() is created (which is a string containing single
quotes), but then you don't print it, you just leave it. So the
interpreter shows you the repr() of that object, enclosing it in double
quotes for simplicity.
--
DaveA
More information about the Tutor
mailing list