str0 = 'abc'
a = bytes(str0, 'utf-8')
print(type(str0),str0)
print(type(a),a)
print('-----------------------')
c = bytes([97,98,99,100])
print(type(c),c)
str1= str(c,"utf-8")
print(type(str1),str1)
输出:
<class 'str'> abc
<class 'bytes'> b'abc'
-----------------------
<class 'bytes'> b'abcd'
<class 'str'> abcd
Python str和bytes的相互转换
最新推荐文章于 2024-07-12 10:52:40 发布