加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python str与bytes之间的转换

发布时间:2020-12-20 10:06:10 所属栏目:Python 来源:网络整理
导读: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) ? ? #
 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读