# coding:utf-8
if __name__ == '__main__':
''' example 1: adopt b'string' Definition bytes type , But it doesn't support Chinese ( If there is Chinese, an error will be reported ) example 2: Can pass string.encoding('utf-8') Converts a string to bytes type ( Chinese compatible ), Reduction by bytes.decode('utf-8') '''
# example 1
b = b'abc123'
print(b) # b'abc123'
print(type(b)) # <class 'bytes'>
# c = b' I abc123' Error
# example 2
c = ' I abc123'
c = c.encode('utf-8')
print(c) # b'\xe6\x88\x91abc123'
print(type(c)) # <class 'bytes'>
print(c.decode('utf-8')) # I abc123
bytes Method
# coding:utf-8
if __name__ == '__main__':
''' example 1:string There are ways bytes Most of them have ( But its string parameter must be bytes type ) '''
b = b'abc123'
print(b.find(b'a')) # 0
c = 'abc123'
print(c.find('a')) # 0
# b.find('a') Error Parameter must be bytes
print(b.replace(b'a', b'f')) # b'fbc123'
print(c.replace('a', 'f')) # fbc123
# b.replace('a', 'f') Error Parameter must be bytes