亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

python2中有一種類(lèi)型叫做unicode型,例

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

兩者返回的類(lèi)型都是unicode型

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

type(u"a") => str型
type("a".decode('utf8')) => 報(bào)錯(cuò),python3不能這樣寫(xiě)

但是python3中多處一種字符串

type(b'132') => byte型

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

相關(guān)文章

最新評(píng)論