bytes定义
# coding:utf-8
if __name__ == '__main__':
'''
例1:通过b'string'定义bytes类型,但不支持中文(出现中文则报错)
例2:可以通过string.encoding('utf-8')将字符串转换成bytes类型(兼容中文),还原通过bytes.decode('utf-8')
'''
# 例1
b = b'abc123'
print(b) # b'abc123'
print(type(b)) # <class 'bytes'>
# c = b'我abc123' Error
# 例2
c = '我abc123'
c = c.encode('utf-8')
print(c) # b'\xe6\x88\x91abc123'
print(type(c)) # <class 'bytes'>
print(c.decode('utf-8')) # 我abc123
bytes方法
# coding:utf-8
if __name__ == '__main__':
'''
例1:string有的方法bytes大部分都有(但其字符串参数必须是bytes类型)
'''
b = b'abc123'
print(b.find(b'a')) # 0
c = 'abc123'
print(c.find('a')) # 0
# b.find('a') Error 参数必须是bytes
print(b.replace(b'a', b'f')) # b'fbc123'
print(c.replace('a', 'f')) # fbc123
# b.replace('a', 'f') Error 参数必须是bytes