Python bytearray/bytes/string区别
目錄
- 一.字節與字符的區別
- 1.字節概念
- 2.字符概念
- 3.字符串概念
- 4.字節串概念
- 二.str / bytes / bytearray 區別
- 三.string 與 bytes / bytearray 相互轉換
- 1.string 經過編碼 encode 轉化成 bytes
- 2.bytes 經過解碼 decode 轉化成 string
- 四.猜你喜歡
零基礎 Python 學習路線推薦 :?Python 學習目錄?>>?Python 基礎入門
一.字節與字符的區別
? ? ??在講解?bytearray?/?bytes?/?string?三者的區別之前,有必要來了解一下字節和字符的區別;
1.字節概念
? ? ??字節(Byte )是計算機信息技術用于計量存儲容量的一種計量單位,作為一個單位來處理的一個二進制數字串,是構成信息的一個小單位。最常用的字節是八位的字節,即它包含八位的二進制數;
- ? ? ??位 ( bit ) 是計算機 內部數據 儲存的最小單位,11001100 是一個八位二進制數;
- ? ? ??字節 ( byte ) 是計算機中 數據處理 的基本單位,習慣上用大寫 ?B ?來表示, 1B ( byte , 字節 ) = 8 bit ( 位 ) ;
2.字符概念
? ? ??字符 是指計算機中使用的字母、數字、字和符號,包括:1、2、3、A、B、C、~!·#¥%……—*()——+等等;
- 一般 utf-8 編碼下,一個漢字 字符 占用 3 個 字節;
- 一般 gbk 編碼下,一個漢字 字符 占用 2 個 字節;
3.字符串概念
? ? ??字符串是字符序列,它是一種抽象的概念,不能直接存儲在硬盤 –?字節串是給計算機看的,給計算機傳輸或者保存的,在 Python 中,程序中的文本都用字符串表示;
4.字節串概念
? ? ??字節串是字節序列,它可以直接存儲在硬盤,?字節串是給計算機看的。它們之間的映射被稱為編碼 / 解碼 – 字符串是給人看的,用來操作的;
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python bytearray/bytes/string區別.py @Time:2021/04/30 08:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""if?__name__?==?"__main__":#?字符串str?轉?字節bytess?=?'猿說python'b?=?s.encode()??#?編碼,默認的是UTF-8print(b)print(type(b))#?字節bytes?轉?字符串strb?=?b'\xe7\x8c\xbf\xe8\xaf\xb4python'.decode(encoding='UTF-8')??#?解碼print(b)print(type(b))''' 輸出結果:b'\xe7\x8c\xbf\xe8\xaf\xb4python' <class 'bytes'> 猿說python <class 'str'> '''二.str / bytes / bytearray 區別
? ? ??1.str 是字符數據(如:文本,給人看的),bytes 和 bytearray 是字節數據(如:二進制數據,給計算機看的),它們都是序列,可以進行迭代遍歷。
? ? ??2.str 和 bytes 是不可變序列,通過 str 類型的通用函數,比如 find 、replace 、islower 等函數修改后實際上是重新創建了新對象;bytearray 是可變序列,可以原處修改字節。
? ? ??3.bytes 和 bytearray 都能使用 str 類型的通用函數,比如find 、replace 、islower 等,不能用的是 str 的格式化操作。
? ? ??4.Python 3.x 中默認 str 是 unicode 格式編碼的,例如 UTF-8 字符集。
三.string 與 bytes / bytearray 相互轉換
1.string 經過編碼 encode 轉化成 bytes
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python bytearray/bytes/string區別.py @Time:2021/04/30 08:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""if?__name__?==?"__main__":s?=?"https://www.codersrc.com"#?將字符串轉換為字節對象b2?=?bytes(s,?encoding='utf8')??#?必須制定編碼格式#?print(b2)#?字符串encode將獲得一個bytes對象b3?=?str.encode(s)b4?=?s.encode()print(b3)print(type(b3))print(b4)print(type(b4))''' 輸出結果:b'https://www.codersrc.com' <class 'bytes'> b'https://www.codersrc.com' <class 'bytes'> '''2.bytes 經過解碼 decode 轉化成 string
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python bytearray/bytes/string區別.py @Time:2021/04/30 08:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""if?__name__?==?"__main__":#?字節對象bb?=?bytes("python教程-猿說python","utf-8")#方案一:s2?=?bytes.decode(b)#?方案二:s3?=?b.decode()print(s2)print(s3)''' 輸出結果:python教程-猿說python python教程-猿說python '''? ? ??注意:如果 bytes 初始化含有中文的字符串必須設置編碼格式,否則報錯:TypeError: string argument without an encoding
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python bytearray/bytes/string區別.py @Time:2021/04/30 08:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""b?=?bytes("猿說python")>>>?b?=?bytes("猿說python") >>>?TypeError:?string?argument?without?an?encoding四.猜你喜歡
本文由博客群發一文多發等運營工具平臺 OpenWrite 發布
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python bytearray/bytes/string区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BugkuCTF-MISC题隐写2
- 下一篇: Python json模块 - Pyth