1. 程式人生 > >[轉]python str與bytes之間的轉換

[轉]python str與bytes之間的轉換

color eth bsp nat alter .com http log com

原文:http://www.cnblogs.com/zqifa/p/python-7.html

# 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 str與bytes之間的轉換