Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, it looks like you're using Python 3, so I'll respond accordingly.</p> <p>Maybe I'm not understanding your question, but it looks like the output you are getting is <em>exactly</em> what you want, <em>except</em> that Chinese characters are wider in your font.</p> <p>So UTF-8 is a red herring, since we are not talking about <em>bytes</em>, we are talking about <em>characters</em>. You are in Python 3, so all strings are Unicode. The underlying byte representation (where each of those Chinese characters is represented by three bytes) is irrelevant.</p> <p>You want to clip or pad each string to exactly 10 characters, and that is working correctly:</p> <pre><code>&gt;&gt;&gt; len('hello, wor') 10 &gt;&gt;&gt; len('hello, 世界 ') 10 &gt;&gt;&gt; len('hello, 世rl') 10 </code></pre> <p>The only problem is that you are looking at it with what appears to be a monospaced font, but which actually <em>isn't</em>. Most monospaced fonts have this problem. All the normal Latin characters have exactly the same width in this font, but the Chinese characters are slightly wider. Therefore, the three characters <code>"世界 "</code> take up more horizontal space than the three characters <code>"wor"</code>. There isn't much you can do about this, aside from either a) getting a font which is truly monospaced, or b) calculating precisely how wide each character is in your font, and adding a number of spaces which approximately takes you to the same horizontal position (this will never be accurate).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload