程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python bytes and its methods

編輯:Python

bytes Definition

# 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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved