[Tutor] A unicode print question

Joel Goldstick joel.goldstick at gmail.com
Fri Jul 30 19:33:17 CEST 2010

I was reading this: http://diveintopython.org/xml_processing/unicode.html

and tried the exercise:

>>> s = u'La Pe\xf1a'         [image: 1]
>>> print s                   [image: 2]
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> print s.encode('latin-1') [image: 3]
La Peña

 [image: 1]<http://diveintopython.org/xml_processing/unicode.html#kgp.unicode.2.1>

But oddly enough, when I typed it into my python shell I did NOT get the
UnicodeError, and I wonder why not:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'La Pe\xf1a'
>>> print s
La Peña
>>> import sys
>>> sys.getdefaultencoding()

Maybe an earlier version of python produces the error, but not 2.6.5?
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100730/861f6b84/attachment.html>

More information about the Tutor mailing list