python字符串str和字节数组相互转化

b = b"Hello, world!"  # bytes object
s = "Hello, world!" # str object print('str --> bytes')
print(bytes(s, encoding="utf8"))
print(str.encode(s)) # 默认 encoding="utf-8"
print(s.encode()) # 默认 encoding="utf-8" print('\nbytes --> str')
print(str(b, encoding="utf-8"))
print(bytes.decode(b)) # 默认 encoding="utf-8"
print(b.decode()) # 默认 encoding="utf-8"
上一篇:codeforces B. Valera and Contest 解题报告


下一篇:【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(五)