目錄
- 一.前言
- 二.Python str / bytes / unicode 區別
- 1.Python2.x 版本中 str / bytes / unicode 區別
- 2.Python3.x 版本中 str / bytes / unicode 區別
- 三.Python string 與 bytes 相互轉換
- 1.string 經過編碼 encode 轉化成 bytes
- 2. bytes 經過解碼 decode 轉化成 string
- 四.猜你喜歡
零基礎 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
'''
四.猜你喜歡
- Python 條件推導式
- Python 串列推導式
- Python 字典推導式
- Python 不定長引數 *argc/**kargcs
- Python 匿名函式 lambda
- Python return 邏輯判斷運算式
- Python is 和 == 區別
- Python 可變資料型別和不可變資料型別
- Python 淺拷貝和深拷貝
- Python 例外處理
- Python 執行緒創建和傳參
- Python 執行緒互斥鎖 Lock
- Python 執行緒時間 Event
- Python 執行緒條件變數 Condition
- Python 執行緒定時器 Timer
- Python 執行緒信號量 Semaphore
- Python 執行緒障礙物件 Barrier
- Python 執行緒佇列 Queue – FIFO
- Python 執行緒佇列 LifoQueue – LIFO
- Python 執行緒優先佇列 PriorityQueue
- Python 執行緒池 ThreadPoolExecutor(一)
- Python 執行緒池 ThreadPoolExecutor(二)
- Python 行程 Process 模塊
- Python 行程 Process 與執行緒 threading 區別
- Python 行程間通信 Queue / Pipe
- Python 行程池 multiprocessing.Pool
- Python GIL 鎖
未經允許不得轉載:猿說編程 ? Python str / bytes / unicode 區別詳解
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288246.html
標籤:Python
