Python function-bytearray

Source: Internet
Author: User
Python function-bytearray

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

Chinese description:

Bytearray ([source [, encoding [, errors]) returns a byte array. The Bytearray type is a variable sequence, and the value range of elements in the sequence is [0,255].


Parameter source:


If source is an integer, an initialized array with the source length is returned;


If source is a string, the string is converted to a byte sequence based on the specified encoding;


If source is an iteration type, the element must be an integer in [0,255;


If source is the same as the buffer interface, this object can also be used to initialize bytearray ..


Version: new version after python2.6. It can also be used in python3!


English description:

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.

Example:

>>> a = bytearray(3)>>> abytearray(b'\x00\x00\x00')>>> a[0]  >>> a[1]  >>> a[2]  >>> b = bytearray("abc")>>> bbytearray(b'abc')>>> b[0]   >>> b[1]  >>> b[2]  >>> c = bytearray([1, 2, 3])>>> cbytearray(b'\x01\x02\x03')>>> c[0]  >>> c[1]  >>> c[2]  >>> d = bytearray(buffer("abc"))>>> dbytearray(b'abc')>>> d[0]  >>> d[1]  >>> d[2]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.