1. 程式人生 > 其它 >Python str / bytes / unicode 區別詳解 - Python零基礎入門教程

Python str / bytes / unicode 區別詳解 - Python零基礎入門教程

目錄

零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門

一.前言

在講解 str / bytes / unicode 區別之前首先要明白位元組和字元的區別,請參考:

bytearray / bytes / string 區別 中對位元組和字元有清晰的講解,最重要是明白:

  • 字元 str 是給人看的,例如:文字儲存的內容,用來操作的;
  • 位元組 bytes 是給計算機看的,例如:二進位制資料,給計算機傳輸或者儲存的;

二.Python str / bytes / unicode 區別

1.Python2.x 版本中 str / bytes / unicode 區別

Python2.x 版本中 str 跟 bytes 是等價的;值得注意的是:bytes 跟 unicode 是等價的,詳情見下圖

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說程式設計
@Blog(個人部落格地址): www.codersrc.com
@File:Python str / bytes / unicode 區別詳解.py
@Time:2021/05/09 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!

"""


s1 = u"Hello, World!"
s2 = "Hello, World!"
print(type(s1))
print(type(s2))


'''
輸出:

<type 'unicode'>
<type 'str'>
'''

2.Python3.x 版本中 str / bytes / unicode 區別

Python3.x 版本中 str 跟 unicode 是等價的;值得注意的是:bytes 跟 unicode 是不等價的,詳情見下圖

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說程式設計
@Blog(個人部落格地址): www.codersrc.com
@File:Python str / bytes / unicode 區別詳解.py
@Time:2021/05/09 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!

"""

s1 = u"Hello, World!"
s2 = "Hello, World!"
print(type(s1))
print(type(s2))


'''
輸出:

<class 'str'>
<class 'str'>
'''

三.Python string 與 bytes 相互轉換

1.string 經過編碼 encode 轉化成 bytes

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說程式設計
@Blog(個人部落格地址): www.codersrc.com
@File:Python str / bytes / unicode 區別詳解.py
@Time:2021/05/09 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!

"""


s = "www.codersrc.com"
#將字串轉換為位元組物件
b2 = bytes(s,encoding='utf8') #必須制定編碼格式
# print(b2)

#方法一:字串encode將獲得一個bytes物件
b3 = str.encode(s)
#方法二:字串encode將獲得一個bytes物件
b4 = s.encode()
print(b3)
print(type(b3))
print(b4)
print(type(b4))

'''
輸出結果:

b'www.codersrc.com'
<class 'bytes'>
b'www.codersrc.com'
<class 'bytes'>
'''

2. bytes 經過解碼 decode 轉化成 string

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說程式設計
@Blog(個人部落格地址): www.codersrc.com
@File:Python str / bytes / unicode 區別詳解.py
@Time:2021/05/09 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!

"""


# 位元組物件b2
    # 如果含有中文,必須制定編碼格式,否則報錯TypeError: string argument without an encoding
    b2 = bytes("猿說python", encoding='utf8')

    # 方法二:bytes物件decode將獲得一個字串
    s2 = bytes.decode(b2)
    # 方法二:bytes物件decode將獲得一個字串
    s3 = b2.decode()
    print(s2)
    print(s3)

'''
輸出結果:

猿說python
猿說python
'''

四.猜你喜歡

  1. Python 條件推導式
  2. Python 列表推導式
  3. Python 字典推導式
  4. Python 不定長引數 *argc/**kargcs
  5. Python 匿名函式 lambda
  6. Python return 邏輯判斷表示式
  7. Python is 和 == 區別
  8. Python 可變資料型別和不可變資料型別
  9. Python 淺拷貝和深拷貝
  10. Python 異常處理
  11. Python 執行緒建立和傳參
  12. Python 執行緒互斥鎖 Lock
  13. Python 執行緒時間 Event
  14. Python 執行緒條件變數 Condition
  15. Python 執行緒定時器 Timer
  16. Python 執行緒訊號量 Semaphore
  17. Python 執行緒障礙物件 Barrier
  18. Python 執行緒佇列 Queue – FIFO
  19. Python 執行緒佇列 LifoQueue – LIFO
  20. Python 執行緒優先佇列 PriorityQueue
  21. Python 執行緒池 ThreadPoolExecutor(一)
  22. Python 執行緒池 ThreadPoolExecutor(二)
  23. Python 程序 Process 模組
  24. Python 程序 Process 與執行緒 threading 區別
  25. Python 程序間通訊 Queue / Pipe
  26. Python 程序池 multiprocessing.Pool
  27. Python GIL 鎖

未經允許不得轉載:猿說程式設計 » Python str / bytes / unicode 區別詳解

本文由部落格 - 猿說程式設計 猿說程式設計 釋出!