BigZ
BigZ

Reputation: 886

.format() and unicode strings

i am trying to format a bunch of strings in python 2.7.6. Everything works properly until an unicode sign shows up. This short example shows my problem:

    a = 'ö'
    b = 'd'
    c = 'e'

    print('{:2}{:2}{:2}').format(a, b, c)

The result is:

öd e

But it should be:

ö d e

Tried a lot of stuff with encoding, decoding, unicodedata.normalize, but nothing seems to work. Anyone got an idea what i am doing wrong? Thanks for help and please excuse for my bad english. Greetz,

BigZ

Upvotes: 0

Views: 73

Answers (1)

mhawke
mhawke

Reputation: 87124

Does this do the trick for you?

>>> a = 'ö'
>>> b = 'd'
>>> c = 'e'
>>> print(u'{:2}{:2}{:2}'.format(a.decode('utf8'), b, c))
ö d e

This assumes that your data is utf8 encoded. Note that the format string is unicode.

Also, this doesn't seem to be a problem in Python 3.

Upvotes: 1

Related Questions