python3中字符串和bytes互相转换

 

1.字符串转成bytes:str.encode('utf-8') 

示例1:

>>>'abc'.encode('utf-8')

>>>b'abc'

>>>'123abc'.encode('utf-8')

>>>b'123abc'

>>>'我爱python'.encode('utf-8')

>>>b'\xe6\x88\x91\xe7\x88\xb1python'

2.bytes转成bytes:bytes.decode('utf-8') 

示例2:

>>>b'abc'.decode('utf-8')

>>>abc

>>>b'\xe6\x88\x91\xe7\x88\xb1python'.decode('utf-8')

>>>'我爱python'

你可能感兴趣的:(python基础语法,学习笔记)