擅长:python、mysql、java
<p>在Python 3中,所有字符串都是Unicode字符序列。有一个保存原始字节的<code>bytes</code>类型。</p>
<p>在Python 2中,字符串可以是<code>str</code>类型或<code>unicode</code>类型。你可以通过这样的代码来判断:</p>
<pre><code>def whatisthis(s):
if isinstance(s, str):
print "ordinary string"
elif isinstance(s, unicode):
print "unicode string"
else:
print "not a string"
</code></pre>
<p>这并不区分“Unicode或ASCII”;它只区分Python类型。Unicode字符串可以由ASCII范围内的纯字符组成,bytestring可以包含ASCII、编码的Unicode甚至非文本数据。</p>