Unicode vs UTF-8 confusion in Python / Django?
<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p>

<blockquote>
  <p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) will return a Unicode string, and str(p) will return a normal string, with characters encoded as UTF-8.</p>
</blockquote>

<p>Now, I'm confused because afaik Unicode is not any particular representation, so what is a "Unicode string" in Python? Does that mean UCS-2? Googling turned up <a href="http://www.reportlab.com/i18n/python_unicode_tutorial.html">this "Python Unicode Tutorial"</a> which boldly states</p>

<blockquote>
  <p>Unicode is a two-byte encoding which covers all of the world's common writing systems.</p>
</blockquote>

<p>which is plain wrong, or is it? I have been confused many times by character set and encoding issues, but here I'm quite sure that the documentation I'm reading is confused. Does anybody know what's going on in Python when it gives me a "Unicode string"?</p>

<blockquote>
  <p>what is a "Unicode string" in Python? Does that mean UCS-2?</p>
</blockquote>

<p>Unicode strings in Python are stored internally either as UCS-2 (fixed-length 16-bit representation, almost the same as UTF-16) or UCS-4/UTF-32 (fixed-length 32-bit representation). It's a compile-time option; on Windows it's always UTF-16 whilst many Linux distributions set UTF-32 (âwide modeâ) for their versions of Python.</p>

<p>You are generally not supposed to care: you will see Unicode code-points as single elements in your strings and you won't know whether they're stored as two or four bytes. If you're in a UTF-16 build and you need to handle characters outside the Basic Multilingual Plane you'll be Doing It Wrong, but that's still very rare, and users who really need the extra characters should be compiling wide builds.</p>

<blockquote>
  <p>plain wrong, or is it?</p>
</blockquote>

<p>Yes, it's quite wrong. To be fair I think that tutorial is rather old; it probably pre-dates wide Unicode strings, if not Unicode 3.1 (the version that introduced characters outside the Basic Multilingual Plane).</p>

<p>There is an additional source of confusion stemming from Windows's habit of using the term âUnicodeâ to mean, specifically, the UTF-16LE encoding that NT uses internally. People from Microsoftland may often copy this somewhat misleading habit.</p>
