[Tutor] encoding

Kent Johnson kent37 at tds.net
Tue Sep 12 03:29:07 CEST 2006


Jose P wrote:
> watch this example:
> 
>>>> a=['lula', 'cação']
>>>> print a
> ['lula', 'ca\xc3\xa7\xc3\xa3o']
>>>> print a[1]
> cação
> 
> 
> when i print the list the special characters are not printed correctly! 

When you print a list, it uses repr() to format the contents of the 
list; when you print an item directly, str() is used. For a string 
containing non-ascii characters, the results are different.
> 
> But if i print only the list item that has the special charaters it runs
> OK.
> 
> How do i get list print correctly?

You will have to do the formatting your self. A simple solution might be
for x in a:
   print x

If you want exactly the list formatting you have to work harder. Try 
something like
"['" + "', '".join([str(x) for x in a]) + "']"

Kent



More information about the Tutor mailing list