站长资源脚本专栏

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

整理:jimmy2025/3/7浏览2
简介实例如下:# bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, encoding = "utf8") # bytes to str str(b, encoding = "utf-8")

实例如下:

# bytes object 
b = b"example" 

# str object 
s = "example" 

# str to bytes 
bytes(s, encoding = "utf8") 

# bytes to str 
str(b, encoding = "utf-8") 

# an alternative method 
# str to bytes 
str.encode(s) 

# bytes to str 
bytes.decode(b) 

以上这篇python字符串str和字节数组相互转化方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。