Problem with format string and unicode

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 28 18:25:50 EDT 2008


En Fri, 28 Mar 2008 17:13:00 -0300, Hans Martin <HMartin1 at gmx.net>  
escribió:

> this is probably a trivial problem, but a google search for "python"
> and "unicode" and" format string" gives too many hits: If I specify
> e.g. "%20s" in a format string and the string value contains UTF-8
> stuff (differing number of bytes per character), the length of the
> resulting string (in characters) varies. How can I fix this?

py> x = u"¡año único!"
py> len(x)
11
py> len(x.encode("utf-8"))
14
py> f = u"%20s" % x
py> f
u'         \xa1a\xf1o \xfanico!'
py> print f
          ¡año único!
py> len(f)
20
py> len(f.encode("utf-8"))
23

-- 
Gabriel Genellina




More information about the Python-list mailing list