目錄
- 一.位元組與字符的區別
- 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 ( 位 ) ;**
1 KB = 1024 B(位元組);
1 MB = 1024 KB; (2^10 B)
1 GB = 1024 MB; (2^20 B)
1 TB = 1024 GB; (2^30 B)
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 轉 位元組bytes
s = '猿說python'
b = s.encode() # 編碼,默認的是UTF-8
print(b)
print(type(b))
# 位元組bytes 轉 字串str
b = 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__":
# 位元組物件b
b = 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
四.猜你喜歡
- Python for 回圈
- Python 字串
- Python 串列 list
- Python 元組 tuple
- Python 字典 dict
- Python 條件推導式
- Python 串列推導式
- Python 字典推導式
- Python 函式宣告和呼叫
- Python 不定長引數 *argc/**kargcs
- Python 匿名函式 lambda
- Python return 邏輯判斷運算式
- Python 字串/串列/元組/字典之間的相互轉換
- Python 區域變數和全域變數
- Python type 函式和 isinstance 函式區別
- Python is 和 == 區別
- Python 可變資料型別和不可變資料型別
- Python 淺拷貝和深拷貝
未經允許不得轉載:猿說編程 ? Python bytearray/bytes/string 區別
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288255.html
標籤:其他
下一篇:作業流專案一般都有哪些模塊?
