Python中的str与bytes之间的转换的三种方法

# 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)

原文取自:https://www.cnblogs.com/zqifa/p/python-7.html

posted @ 2018-11-24 14:23  开始认识  阅读(11819)  评论(0编辑  收藏  举报