bytearray([source [, encoding [, errors]]])

中文说明:

bytearray([source [, encoding [, errors]]])返回一个byte数组。Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。

参数source:

如果source为整数,则返回一个长度为source的初始化数组;

如果source为字符串,则按照指定的encoding将字符串转换为字节序列;

如果source为可迭代类型,则元素必须为[0 ,255]中的整数;

如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。

版本:在python2.6后新引入,在python3中同样可以使用!

英文说明:

Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the str type has, see String Methods.

The optional source parameter can be used to initialize the array in a few different ways:

If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

If it is an integer, the array will have that size and will be initialized with null bytes.

If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

New in version 2.6.

实例演示:>>> a = bytearray(3)

>>> a

bytearray(b'\x00\x00\x00')

>>> a[0]

>>> a[1]

>>> a[2]

>>> b = bytearray("abc")

>>> b

bytearray(b'abc')

>>> b[0]

>>> b[1]

>>> b[2]

>>> c = bytearray([1, 2, 3])

>>> c

bytearray(b'\x01\x02\x03')

>>> c[0]

>>> c[1]

>>> c[2]

>>> d = bytearray(buffer("abc"))

>>> d

bytearray(b'abc')

>>> d[0]

>>> d[1]

>>> d[2]

python中bytearray函数_python函数之bytearray用法详解相关推荐

  1. Python中第三方库Requests库的高级用法详解

    Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...

  2. python中字符串乘法_python leetcode 字符串相乘实例详解

    给定两个以字符串形式表示的非负整数 num1 和  num2 ,返回  num1 和  num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", ...

  3. python defaultdict 类属性_Python collections.defaultdict模块用法详解

    Python中通过Key访问字典,当Key不存在时,会引发'KeyError'异常.为了避免这种情况的发生,可以使用collections类中的defaultdict()方法来为字典提供默认值. 语法 ...

  4. python中config命令_python的logging.config使用详解

    python的log模块是日常使用最多的模块之一,  简单的使用这里就不多说了,见(http://blog.csdn.net/jianhong1990/article/details/17475717 ...

  5. Python函数(函数定义、函数调用)用法详解

    函数 函数就是一段封装好的,可以重复使用的代码,它使得我们的程序更加模块化,不需要编写大量重复的代码. 函数可以提前保存起来,并给它起一个独一无二的名字,只要知道它的名字就能使用这段代码.函数还可以接 ...

  6. python数组相减_对Python 中矩阵或者数组相减的法则详解

    对Python 中矩阵或者数组相减的法则详解 最近在做编程练习,发现有些结果的值与答案相差较大,通过分析比较得出结论,大概过程如下: 定义了一个计算损失的函数: def error(yhat,labe ...

  7. 站长在线Python精讲:在Python中使用正则表达式的sub()方法替换字符串详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中使用正则表达式的sub()方法替换字符串详解>. 在Python中使用正则表达式的sub()方法替换字符串. ...

  8. Find_in_set()函数的使用及in()用法详解

    这篇文章主要介绍了mysql中find_in_set()函数的使用以及in()用法详解,需要的朋可以参考下 MySQL手册中find_in_set函数的语法解释: FIND_IN_SET(str,st ...

  9. python中requests库的用途-python中requests库session对象的妙用详解

    在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有时候需要保持一些共用的数据,例如cookies信息. 妙用1 requests库的session对象能够帮我们跨请求保持某些参数,也 ...

  10. python os模块安装方法_基于python中pygame模块的Linux下安装过程(详解)

    一.使用pip安装Python包 大多数较新的Python版本都自带pip,因此首先可检查系统是否已经安装了pip.在Python3中,pip有时被称为pip3. 1.在Linux和OS X系统中检查 ...

最新文章

  1. 新手科普 | 探索机器学习模型,保障账户安全
  2. 01Vue.js快速入门(一)Vue概念及Helloworld
  3. 打印容器_喷墨打印MnO?制备微型超级电容器
  4. windows下使用Hibernate连接Mycat例子
  5. ***Xcode Interface Builder或Storyboard中可建立那两种连接?
  6. Andoid 采用ListView三种显示数据列表
  7. 七步从Angular.JS菜鸟到专家(1):如何开始
  8. mysql 5 安装过程
  9. ActiveMQ学习-Network connectors JAVA代码实现
  10. 网络舆情监测TOOM
  11. 系统角色权限设计与实现
  12. 构建数据指标预警系统
  13. 解决IE上登陆oracle OEM时报:“证书错误,导航已阻止”的错误
  14. jQuery图片播放插件ColorBox使用方法
  15. 如何禁止 Google Chrome 自动更新 (macOS, Linux, Windows)
  16. typora-数学符号
  17. [精品教程,新老鸟必读]:极限装机、优化全攻略(新手优化教程)
  18. 子集生成算法 之 位向量法和增量构造法
  19. 贝叶斯公式的理解及简单推导
  20. 汽车众筹系统:加速建设汽车金融新生态!

热门文章

  1. windows10 使用小技巧
  2. MacOS M1 使用 Homebrew 安装 Mysql
  3. python进度条打印
  4. 云服务器搭建与部署【Unity网络编程(一)】
  5. java中字符和字节的区别是什么?
  6. 笔记本电脑拆解时笔记本电脑的螺丝有了磁力怎么办?哪些注意事项?
  7. HUAWEI华为笔记本电脑MateBook X Pro 2021款MACHD-WF恢复原装出厂OEM原厂系统含F10恢复功能
  8. 微信小程序全局变量globalData
  9. 2021年安全员-B证(江西省)考试APP及安全员-B证(江西省)考试试卷
  10. React下MarkDown在线预览