python字符串str和字节数组相互转化 发表于 2020-10-29 | 字符串转字节数组1234567s = "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" 字节数组转字符串1234567b = b"Hello, world!" # bytes object #一共有三种方式 任选其一print('\nbytes --> str')print(str(b, encoding="utf-8")) print(bytes.decode(b)) # 默认 encoding="utf-8"print(b.decode()) # 默认 encoding="utf-8" 本文为作者原创 转载时请注明出处 谢谢 乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站