python str与bytes之间的转换

前端之家收集整理的这篇文章主要介绍了python str与bytes之间的转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 1 # bytes object  
 2 b = b"example"
 3 
 4  str object  
 5 s = "  
 6 
 7  str to bytes  
 8 sb = bytes(s,encoding = utf8")  
 9 
10  bytes to str  
11 bs = str(b,1)">12 
13  an alternative method  
14 15 sb2 = str.encode(s)  
16 
17 18 bs2 = bytes.decode(b)

 

 

# bytes object
b = b"example"

# str object
s = "example"

# str to bytes
sb = bytes(s,encoding = "utf8")

# bytes to str
bs = str(b,encoding = "utf8")

# an alternative method
# str to bytes
sb2 = str.encode(s)

# bytes to str
bs2 = bytes.decode(b)

猜你在找的Python相关文章