Formatting string with accented characters for printing

Chris Angelico rosuav at gmail.com
Sun Jan 18 19:13:54 CET 2015


On Mon, Jan 19, 2015 at 3:04 AM, Jerry Rocteur <jerry.rocteur at gmail.com> wrote:
>
> I've tried both:
>
>     print '{0:2} {1:25} {2} '.format( cnt, nam[num].encode('utf-8'),
> steps[ind1])
>     print "%3d %-25s %-7s" % ( cnt, nam[num].encode('utf-8'), steps[ind1])
>
> I've searched but I can't see a solution..
>
> I guess it is the way I'm printing nam[num].encode('utf-8') perhaps I
> have to convert it first ?

Do the formatting on a Unicode string, and then encode the entire
string at the end. Or, better still, just work entirely with Unicode
all the way; if you switch to Python 3.4 (or anything else in the 3.x
line), print() takes Unicode strings. This kind of thing becomes easy.

ChrisA



More information about the Python-list mailing list