代碼總結(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中有一種類型叫做unicode型,例
type(u"a") => str型
type("a".decode('utf8')) => unicode型
兩者返回的類型都是unicode型
而在python3中,所有的字符串都是unicode,所以就不存在單獨(dú)的unicode型,全部都是字符串型
type(u"a") => str型
type("a".decode('utf8')) => 報(bào)錯(cuò),python3不能這樣寫
但是python3中多處一種字符串
type(b'132') => byte型
以上就是相關(guān)的知識點(diǎn)內(nèi)容,如果大家有任何補(bǔ)充可以聯(lián)系腳本之家小編。
- Python 字符串處理特殊空格\xc2\xa0\t\n Non-breaking space
- 基于python3實(shí)現(xiàn)倒敘字符串
- Python日期格式和字符串格式相互轉(zhuǎn)換的方法
- 通過python檢測字符串的字母
- python如何把字符串類型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如何訪問字符串中的值
- python3 字符串知識點(diǎn)學(xué)習(xí)筆記
- Python輸出指定字符串的方法
- python求一個(gè)字符串的所有排列的實(shí)現(xiàn)方法
- 詳解字符串在Python內(nèi)部是如何省內(nèi)存的
- Python字符串中刪除特定字符的方法
- Python拼接字符串的7種方式詳解
相關(guān)文章
python Tkinter實(shí)時(shí)顯示數(shù)據(jù)功能實(shí)現(xiàn)
這篇文章主要介紹了python Tkinter實(shí)時(shí)顯示數(shù)據(jù)功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
python爬蟲開發(fā)之urllib模塊詳細(xì)使用方法與實(shí)例全解
這篇文章主要介紹了python爬蟲開發(fā)之urllib模塊詳細(xì)使用方法與實(shí)例全解,需要的朋友可以參考下2020-03-03
基于python實(shí)現(xiàn)微信收紅包自動化測試腳本(測試用例)
這篇文章主要介紹了基于python實(shí)現(xiàn)微信收紅包自動化測試腳本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
基于django和dropzone.js實(shí)現(xiàn)上傳文件
這篇文章主要介紹了基于django和dropzone.js實(shí)現(xiàn)上傳文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(10):webpy框架
webpy小巧,簡單,實(shí)用,可以快速的完成簡單的web頁面。這里根據(jù)webpy Cookbook簡要的介紹一下webpy框架,需要的朋友可以參考下2014-06-06

