python 字符串和字節數互轉


在 python  中字符 是 str 類型, 字節是 bytes 類型

b = b'hello'  # bytes 字節  
s= 'hello'   # str 字符串

可通過 type() 檢查是什么類型 或者 isinstance(),如下:

type('hello, world')
#<class 'str'>
isinstance('hello, world', str)
#True
type(b'hello, world'
) # <class 'bytes'> isinstance(b"hello,world", bytes) # True

 

 

1.字符串轉字節:

bytes('hello, world', encoding="utf8")# b'hello, world'

 

2. 字節轉字符串

str(b'hello, world', encoding="utf-8") # hello, world

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM