python bytes

Python bytes() is a built-in function. This function returns bytes object that is an immutable sequence of integers in the range 0 <= x < 256.

Python bytes()是一个内置函数。 此函数返回字节对象,该对象是不可变的整数序列,范围为0 <= x <256。

Python bytes() (Python bytes())

Python bytes() function syntax is:

Python bytes()函数语法为:

class bytes(]])

source is used to initialize the bytes object. This is an optional argument.

source用于初始化bytes对象。 这是一个可选参数。

encoding is optional unless source is string. It’s used to convert the string to bytes using str.encode() function.

除非source是string,否则encoding是可选的。 它用于使用str.encode()函数将字符串转换为字节。

errors is optional parameter. It’s used if the source is string and encoding fails due to some error.

errors是可选参数。 如果源是字符串并且由于某些错误而编码失败,则使用它。

There are some specific rules followed by bytes() function depending on the type of source.

根据源类型,有一些特定的规则,后跟bytes()函数。

  • If no argument is passed, empty bytes object is returned.如果未传递任何参数,则返回空字节对象。
  • If source is integer, it initializes the bytes object with array of given length with null values.如果source是整数,它将使用具有空值的给定长度的数组初始化bytes对象。
  • If source is string, encoding is mandatory and it’s used to convert string to byte array.如果source是字符串,则编码是强制性的,用于将字符串转换为字节数组。
  • If source is iterable, such as list, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.如果source是可迭代的,例如list ,则它必须是0 <= x <256范围内的整数的可迭代对象,这些整数用作数组的初始内容。

Overall bytes() function is very similar to bytearray() function.

总体而言,bytes()函数与bytearray()函数非常相似。

Let’s look at some of the examples of bytes() function.

让我们看一下bytes()函数的一些示例。

没有参数的bytes() (bytes() with no arguments)

b = bytes()
print(b)

Output: b''

输出: b''

具有字符串和不变性的bytes() (bytes() with string and immutability)

# string to bytes
# encoding is mandatory, otherwise "TypeError: string argument without an encoding"
b = bytes('abc', 'UTF-8')
print(b)# Below code will throw error:
# TypeError: 'bytes' object does not support item assignment
# b[1] = 65  # immutable

Output: b'abc'

输出: b'abc'

Note that if we try to change the element value in the bytes object, we will get an error message as shown in the above comment.

请注意,如果尝试更改bytes对象中的元素值,则会收到一条错误消息,如上面的注释所示。

具有int参数的bytes() (bytes() with int argument)

# bytes of given size, elements initialized to null
b = bytes(5)
print(b)

Output: b'\x00\x00\x00\x00\x00'

输出: b'\x00\x00\x00\x00\x00'

具有可迭代的bytes() (bytes() with iterable)

# bytes from iterable
b = bytes([1, 2, 3])
print(b)

Output: b'\x01\x02\x03'

输出: b'\x01\x02\x03'

That’s all for a quick guide of python bytes() function.

这就是python bytes()函数的快速指南。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22747/python-bytes

python bytes

python bytes_Python bytes()相关推荐

  1. python字符串encode()decode()

    Python字符串encode() (Python String encode()) Python string encode() function is used to encode the str ...

  2. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  3. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  4. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  5. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  6. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  7. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

  8. Python字符串splitlines()

    Python String splitlines() Python字符串splitlines() Python String splitlines() function returns the lis ...

  9. Python字符串join()方法

    Python string join() method creates a string from an iterable. It joins all the iterable elements wi ...

最新文章

  1. 是时候搁置Grunt,耍一耍gulp了
  2. HVM guest配置文件
  3. leetcode111 爬楼梯 python实现
  4. SAP Hybris Enterprise Commerce的一些有用链接 - 保证持续更新
  5. api自动化_如何在不增加人员的情况下自动化API安全程序
  6. vue 报错error: ‘to‘ is defined but never used (no-unused-vars)
  7. 有位哥们坚持不用微服务架构,被老板踢出了群聊···
  8. 前后端分离的项目,如何解决登录问题
  9. 你真明白 Python 装饰器么?
  10. 苹果手机怎么在照片上添加文字_手机照片如何添加花样文字?原来方法这么简单!...
  11. 【java笔记】网络编程概述
  12. C++Builder 解决绘图闪动问题
  13. 跳过密码卸载OfficeScan
  14. wxnativecallback.php,ectouch开发 | 糊涂虫
  15. C语言学习方法(详细,适合初学者学习食用)
  16. 微信小程序:选择图片、上传图片(应用:切换用户头像)及过程中遇到的问题
  17. 【Linux】常用配置文件路径
  18. 以下选项属于python哲学内容的是_[南开大学]18秋学期(清考)《哲学与人生》在线作业...
  19. 百度地图离线开发V2.0
  20. 智能客服机器人的场景应用及功能有哪些

热门文章

  1. 采样干扰十大滤波算法程序大全
  2. 玩转Eclipse1--基本知识与配置
  3. 关于CNDotText的配置
  4. [转载] pandas中Series数组创建方法
  5. java获取当前行数
  6. 基于docker的spark-hadoop分布式集群之二: 环境测试
  7. 201521123058 软工阅读第二次作业
  8. Block和inline元素对比
  9. php 正则匹配 文件,php – 正则表达式匹配.htaccess中的一系列文件类型
  10. python学习模型_python学习笔记(IO模型)