在 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

 

posted on 2019-01-11 17:07  浅唱年华1920  阅读(1399)  评论(0编辑  收藏  举报