<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/" rel="noreferrer">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" rel="noreferrer">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>

[{'answer_id': 22157, 'author': 'Jonathan Works', 'author_id': 309844, 'author_profile': 'https://Stackoverflow.com/users/309844', 'pm_score': 0, 'selected': False, 'text': '<p>Python stores Unicode as UTF-16. str() will return the UTF-8 representation of the UTF-16 string.</p>\n'}, {'answer_id': 22168, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': -1, 'selected': False, 'text': '<p>From <a href="http://en.wikipedia.org/wiki/UTF-8" rel="nofollow noreferrer">Wikipedia on UTF-8</a>: </p>\n\n<blockquote>\nUTF-8 (8-bit UCS/Unicode Transformation Format) is a <strong>variable-length character encoding for Unicode. It is able to represent any character in the Unicode standard</strong>, yet the initial encoding of byte codes and character assignments for UTF-8 is backwards compatible with ASCII. For these reasons, it is steadily becoming the preferred encoding for e-mail, web pages[1], and other places where characters are stored or streamed.\n</blockquote>\n\n<p>So, it\'s anywhere between one and four bytes depending on which character you wish to represent within the realm of Unicode.</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Unicode" rel="nofollow noreferrer">From Wikipedia on Unicode:</a></p>\n\n<blockquote>\nIn computing, Unicode is an industry standard allowing computers to consistently represent and manipulate text expressed in <strong>most of the world\'s writing systems</strong>. \n</blockquote>\n\n<p>So it\'s able to represent most (but not all) of the world\'s writing systems. </p>\n\n<p>I hope this helps :)</p>\n'}, {'answer_id': 22206, 'author': 'Ravi Chhabra', 'author_id': 370899, 'author_profile': 'https://Stackoverflow.com/users/370899', 'pm_score': -1, 'selected': False, 'text': '<blockquote>\n  <p>so what is a "Unicode string" in\n  Python?</p>\n</blockquote>\n\n<p>Python \'knows\' that your string is Unicode. Hence if you do regex on it, it will know which is character and which is not etc, which is really helpful. If you did a strlen it will also give the correct result. As an example if you did string count on Hello, you will get 5 (even if it\'s Unicode). But if you did a string count of a foreign word and that string was not a Unicode string than you will have much larger result. Pythong uses the information form the Unicode Character Database to identify each character in the Unicode String. Hope that helps. </p>\n'}, {'answer_id': 22208, 'author': 'Hanno Fietz', 'author_id': 2077, 'author_profile': 'https://Stackoverflow.com/users/2077', 'pm_score': 3, 'selected': False, 'text': '<p>Meanwhile, I did a refined research to verify what the internal representation in Python is, and also what its limits are. "<a href="http://www.cmlenz.net/archives/2008/07/the-truth-about-unicode-in-python" rel="noreferrer" title="The truth about Unicode in Python">The Truth About Unicode In Python</a>" is a very good article which cites directly from the Python developers. Apparently, internal representation is either UCS-2 or UCS-4 depending on a compile-time switch. So Jon, it\'s not UTF-16, but your answer put me on the right track anyway, thanks.</p>\n'}, {'answer_id': 522827, 'author': 'bobince', 'author_id': 18936, 'author_profile': 'https://Stackoverflow.com/users/18936', 'pm_score': 7, 'selected': True, 'text': '<blockquote>\n  <p>what is a "Unicode string" in Python? Does that mean UCS-2?</p>\n</blockquote>\n\n<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>\n\n<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>\n\n<blockquote>\n  <p>plain wrong, or is it?</p>\n</blockquote>\n\n<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>\n\n<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>\n'}]
['https://Stackoverflow.com/questions/22149', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2077/']