Bytearray () function in Python
Bytearray () function in Python returns the bytearray object of a given byte array. How is the syntax of bytearray () function, what parameters does it have and how to use it?
Syntax of bytearray () function:
bytearray([source[, encoding[, errors]]])
The bytearray () function returns a bytearray object which is a variable string (can be modified) of integers in the range 0 <= x <256.
If you want the immutable version use the byte () method.
Parameters of bytearray () function:
Bytearray () function has 3 parameters, all 3 are optional, yes or no.
- source: Initialize byte array /
- encoding: If the source is a string, you must add this parameter to convert, decode the string into bytes.
- errors: If the source is a string, errors will provide action to retrieve when the decoding process fails.
The source parameter can be used to initialize the byte array in the following ways:
What value does the function bytearray () return?
The bytearray () function returns a byte array with a given size and initial values.
Example of bytearray () function:
# Ví dụ 1: source là chuỗi
string1 = "quantrimang.com."
# encoding là 'utf-8'
mang1 = bytearray(string1, 'utf-8')
print(mang1)
# Ví dụ 2: source là số
string2 = 10
mang2 = bytearray(string2)
print(mang2)
#Ví dụ 3: source là list
ListSo = [2,4,6,8,10]
mang3 = bytearray(ListSo)
print(mang3)
When running the above program, we get the following output:
bytearray(b'quantrimang.com.')
bytearray(b'x00x00x00x00x00x00x00x00x00x00')
bytearray(b'x02x04x06x08n')
See more:
- Bool () function in Python
- Ascii () function in Python
- Bin () function in Python
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.