Python3 bytes 函數(shù)

2019-03-26 10:33 更新

Python3 bytes 函數(shù)

Python3 內(nèi)置函數(shù) Python3 內(nèi)置函數(shù)

描述

Python3 bytes 函數(shù)返回一個(gè)新的 bytes 對象,該對象是一個(gè) 0 <= x < 256 區(qū)間內(nèi)的整數(shù)不可變序列。它是 bytearray 的不可變版本。

語法

以下是 bytes 的語法:

class bytes([source[, encoding[, errors]]])

參數(shù)

  • 如果 source 為整數(shù),則返回一個(gè)長度為 source 的初始化數(shù)組;
  • 如果 source 為字符串,則按照指定的 encoding 將字符串轉(zhuǎn)換為字節(jié)序列;
  • 如果 source 為可迭代類型,則元素必須為[0 ,255] 中的整數(shù);
  • 如果 source 為與 buffer 接口一致的對象,則此對象也可以被用于初始化 bytearray。
  • 如果沒有輸入任何參數(shù),默認(rèn)就是初始化數(shù)組為0個(gè)元素。

返回值

返回一個(gè)新的 bytes 對象。

實(shí)例

以下展示了使用 bytes 的實(shí)例:

>>>a = bytes([1,2,3,4])
>>> a
b'\x01\x02\x03\x04'
>>> type(a)
<class 'bytes'>
>>>
>>> a = bytes('hello','ascii')
>>>
>>> a
b'hello'
>>> type(a)
<class 'bytes'>
>>>

Python3 內(nèi)置函數(shù) Python3 內(nèi)置函數(shù)


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號