Python學習筆記之Bytes與Str轉換

# bytes object
b = b"11223344"

# str object
s = "xsophiax"

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

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

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

# bytes to str
bts2 = bytes.decode(b)
print(bts2)

輸出

b'xsophiax'
11223344
b'xsophiax'
11223344

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章