python string str and byte array mutual conversion method

  • 2020-05-27 06:03:39
  • OfStack

Examples are as follows:


# 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) 

Related articles: