1

I have noticed that str.format does not seem to do what is documented with Chinese characters.

Consider:

# -*- coding: utf-8 -*-

from __future__ import print_function

tests={'German': [u'Straße',u'auslösen',u'zerstören'], 
       'French': [u'français',u'américaine',u'épais'], 
       'Chinese': [u'中國的',u'英語',u'美國人']}

for k in tests.keys():
    print(k)
    for v in tests[k]:
        print(u'"{:^15}"'.format(v))  

My understanding, the "{:^15}" format string should be fixed width. However, note the output:

Chinese
"      中國的      "
"      英語       "
"      美國人      "
French
"   français    "
"  américaine   "
"     épais     "
German
"    Straße     "
"   auslösen    "
"   zerstören   "

The field width for the Chinese characters are clearly changing. Nor are the Chinese strings being centered in the 15 character field.

Any idea why this is happening? I have tried both under Py 2.7 and Py 3.3

dawg
  • 98,345
  • 23
  • 131
  • 206
  • It seems possible it has to do with this: http://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms . – BrenBarn Jun 22 '13 at 05:17

1 Answers1

4

Chinese fonts are "full width" fonts and the characters are wider, even in a console window.

See this question for a similar question and answer:
How to control padding of Unicode string containing east Asia characters

There are full-width versions of the ASCII characters if you are mixing Asian and English and trying for alignment, but I don't believe there are full-width versions of the accented characters, at least without a special font.

Community
  • 1
  • 1
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251