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