python - Different encodings used in in "print s" vs "print [s]"? -


when following in ipython notebook

s='½' s print s print [s] 

i see

'\xc2\xbd' ½ ['\xc2\xbd'] 
  1. what's going on here?
  2. how print list of unicode strings? (ie want see ['½'])

edit comments, looks difference "print s" uses s.__str__ , "s", "print [s]" uses it's s.__repr__

you can use repr function create string containing printable representation of list, decode string string-escape encoding returns byte string of string. printing byte string terminal encode automatically it's default encoding (usually utf8) :

>>> print repr([s]).decode('string-escape') ['½'] 

but note since in python 3.x have unicode, don't need use trick :

python 3.4.3 (default, oct 14 2015, 20:28:29)  [gcc 4.8.4] on linux type "help", "copyright", "credits" or "license" more information. >>>  >>> s='½' >>> print ([s]) ['½'] 

for more info python encodings read https://docs.python.org/2.4/lib/standard-encodings.html


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -