title: Aligning Japanese characters in python
body: <p>I have difficulty aligning Japanese characters in python.</p>
  
  <p>Code:</p>
  
  <pre><code>print "{c1}{name:&gt;14s}{c2}{nick_name:&gt;14s}{planes:&gt;16s}".format(
      name=name, nick_name=nick_name, planes=planes, 
      c1=u.color['yellow'], c2=u.color['default']
  )
  </code></pre>
  
  <p>Result:
  <img src="https://i.stack.imgur.com/zSF5F.jpg" alt="enter image description here"></p>
  
  <p>If the string contains english and numbers only, the .format() works fine,as shown on the right.</p>
  
  <p>The aligning goes wrong when encountering Japanese characters, as shown on the left.</p>
  
  <p>Interestingly, when aligning with <code>{name:&gt;14s}</code>:</p>
  
  <ul>
  <li>If "name" contains 4 JP characters, there would be 2 prefix spaces.</li>
  <li>If "name" contains 3 JP characters, there would be 5 prefix spaces.</li>
  <li>If "name" contains 2 JP characters, there would be 8 prefix spaces.</li>
  <li>If "name" contains 0 JP characters, there would be 14 prefix spaces.</li>
  </ul>
  
  <p>It seems like it treat 1 Japanese charater = 3 spaces in this case.</p>
  
  <p><code>{name:&lt;14s}</code> <code>{name:^14s}</code> <code>{name:&gt;14s}</code> all have the behavior mentioned above.</p>
  
  <p>I am using OSX 10.10.2, terminal font is monaco.</p>
  
  <p>Maybe this has something to do with full-width/half-width characters.</p>
  
  <p>Is there anyway to align Japanese characters just like English characters?</p>
  
  <p>Thanks you.</p>
  
  <hr>
  
  <p>Edit: </p>
  
  <p>Ignacio Vazquez-Abrams's answer is indeed the correct way.</p>
  
  <ul>
  <li><p>Everyone who is dealing with unicode in Python should read <a href="http://farmdev.com/talks/unicode/" rel="nofollow noreferrer">the slide</a> he pointed out.</p></li>
  <li><p>The "\u3000" is the full-width space in CJK. See <a href="http://www.fileformat.info/info/unicode/char/3000/index.htm" rel="nofollow noreferrer">this page</a>.</p></li>
  <li><p>Review the <a href="https://docs.python.org/2/library/string.html#grammar-token-fill" rel="nofollow noreferrer">.Format Syntax</a> would also help.</p></li>
  <li><p>I would also like to recommend <a href="https://stackoverflow.com/questions/368805/python-unicodedecodeerror-am-i-misunderstanding-encode#370199">this SO answer</a> which helps me understanding how unicode works in Python.</p></li>
  </ul>
  
  <p>However, if the string contains both half-width and full-width characters, the alignment still goes wrong. A simple workaround is to use all full-width characters.</p>
  
  <p><img src="https://i.stack.imgur.com/htWsh.png" alt="enter image description here"></p>