代碼總結(jié)Python2 和 Python3 字符串的區(qū)別
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)系腳本之家小編。
- Python 字符串處理特殊空格\xc2\xa0\t\n Non-breaking space
- 基于python3實(shí)現(xiàn)倒敘字符串
- Python日期格式和字符串格式相互轉(zhuǎn)換的方法
- 通過(guò)python檢測(cè)字符串的字母
- python如何把字符串類(lèi)型list轉(zhuǎn)換成list
- python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解
- python字符串下標(biāo)與切片及使用方法
- python 實(shí)現(xiàn)字符串下標(biāo)的輸出功能
- python判斷變量是否為int、字符串、列表、元組、字典的方法詳解
- Python基礎(chǔ)之字符串操作常用函數(shù)集合
- python字符串替換re.sub()實(shí)例解析
- Python如何訪(fǎng)問(wèn)字符串中的值
- python3 字符串知識(shí)點(diǎn)學(xué)習(xí)筆記
- Python輸出指定字符串的方法
- python求一個(gè)字符串的所有排列的實(shí)現(xiàn)方法
- 詳解字符串在Python內(nèi)部是如何省內(nèi)存的
- Python字符串中刪除特定字符的方法
- Python拼接字符串的7種方式詳解
相關(guān)文章
python常見(jiàn)字符串處理函數(shù)與用法匯總
這篇文章主要介紹了python常見(jiàn)字符串處理函數(shù)與用法,結(jié)合實(shí)例形式詳細(xì)分析了Python字符串操作函數(shù)find、join、replace及split功能、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-10-10python Tkinter實(shí)時(shí)顯示數(shù)據(jù)功能實(shí)現(xiàn)
這篇文章主要介紹了python Tkinter實(shí)時(shí)顯示數(shù)據(jù)功能實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07python通過(guò)正則查找微博@(at)用戶(hù)的方法
這篇文章主要介紹了python通過(guò)正則查找微博@(at)用戶(hù)的方法,涉及Python正則匹配的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03python爬蟲(chóng)開(kāi)發(fā)之urllib模塊詳細(xì)使用方法與實(shí)例全解
這篇文章主要介紹了python爬蟲(chóng)開(kāi)發(fā)之urllib模塊詳細(xì)使用方法與實(shí)例全解,需要的朋友可以參考下2020-03-03基于python實(shí)現(xiàn)微信收紅包自動(dòng)化測(cè)試腳本(測(cè)試用例)
這篇文章主要介紹了基于python實(shí)現(xiàn)微信收紅包自動(dòng)化測(cè)試腳本,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07基于django和dropzone.js實(shí)現(xiàn)上傳文件
這篇文章主要介紹了基于django和dropzone.js實(shí)現(xiàn)上傳文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(10):webpy框架
webpy小巧,簡(jiǎn)單,實(shí)用,可以快速的完成簡(jiǎn)單的web頁(yè)面。這里根據(jù)webpy Cookbook簡(jiǎn)要的介紹一下webpy框架,需要的朋友可以參考下2014-06-06