在 python  中字符 是 str 类型, 字节是 bytes 类型

b = b'hello'  # bytes 字节  
s= 'hello'   # str 字符串

可通过 type() 检查是什么类型 或者 isinstance(),如下:

type('hello, world')
#<class 'str'>
isinstance('hello, world', str)
#True
type(b'hello, world'
) # <class 'bytes'> isinstance(b"hello,world", bytes) # True

 

 

1.字符串转字节:

bytes('hello, world', encoding="utf8")# b'hello, world'

 

2. 字节转字符串

str(b'hello, world', encoding="utf-8") # hello, world

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-17
  • 2021-07-09
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
相关资源
相似解决方案