欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

代碼總結(jié)Python2 和 Python3 字符串的區(qū)別

 更新時間:2020年01月28日 08:40:53   作者:moon__light  
在本篇文章里小編給大家整理的是一篇關(guān)于Python2 和 Python3 字符串的區(qū)別以及實例代碼,需要的朋友們學(xué)習(xí)下。

Python2

>>> 
>>> isinstance(b'abc', bytes)
True
>>> 
>>> isinstance(b'abc', str)
True
>>> 
>>> isinstance('abc', str)
True
>>> 
>>> isinstance('abc', bytes)
True
>>> 
>>> 
>>> 
>>> 'abc'.startswith('ab')
True
>>> 
>>> b'abc'.startswith('ab'.encode())
True
>>> 
>>> b'abc'.startswith('ab')
True
>>> 
>>> 'abc'.startswith('ab'.encode())
True
>>>

Python3

>>> 
>>> isinstance(b'abc', bytes)
True
>>> 
>>> isinstance(b'abc', str)
False
>>> 
>>> isinstance('abc', str)
True
>>> 
>>> isinstance('abc', bytes)
False
>>> 
>>> 
>>> 
>>> 'abc'.startswith('ab')
True
>>> 
>>> b'abc'.startswith('ab'.encode())
True
>>> 
>>> b'abc'.startswith('ab')
Traceback (most recent call last):
 File "<pyshell#25>", line 1, in <module>
  b'abc'.startswith('ab')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
>>> 
>>> 'abc'.startswith('ab'.encode())
Traceback (most recent call last):
 File "<pyshell#27>", line 1, in <module>
  'abc'.startswith('ab'.encode())
TypeError: startswith first arg must be str or a tuple of str, not bytes
>>>

擴展學(xué)習(xí)

python2中有一種類型叫做unicode型,例

type(u"a") => str型
type("a".decode('utf8')) => unicode型

兩者返回的類型都是unicode型

而在python3中,所有的字符串都是unicode,所以就不存在單獨的unicode型,全部都是字符串型

type(u"a") => str型
type("a".decode('utf8')) => 報錯,python3不能這樣寫

但是python3中多處一種字符串

type(b'132') => byte型

以上就是相關(guān)的知識點內(nèi)容,如果大家有任何補充可以聯(lián)系腳本之家小編。

相關(guān)文章

最新評論