A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/python_bytearray_function.htm below:

Python bytearray() function

Python bytearray() function

The Python bytearray() function returns a new array of bytes. It is a mutable sequence of integers in the range 0 <= x < 256. This function can convert a specified object into a bytearray object or it can create an empty bytearray object of the required size. It is one of the built-in functions in Python.

The byte array can be created from the following sources −

Syntax

Following is the syntax of the Python bytearray() function −

bytearray(source)
or,
bytearray(source, encoding)
or,
bytearray(source, encoding, errors)
Parameters

The Python bytearray() function accepts three parameters, all of which are optional −

Return Value

The Python bytearray() function returns a new array of bytes.

bytearray() function Examples

Practice the following examples to understand the use of bytearray() function in Python:

Example: Use of bytearray() Function

The following example shows the usage of Python bytearray() function. Here we are creating an empty bytearray.

empByte_array = bytearray()
print("It is an example of empty bytearray:", empByte_array)

When we run above program, it produces following result −

It is an example of empty bytearray: bytearray(b'')
Example: Convert String to bytearray Object Using bytearray()

In the code below, we are converting a given string into array of bytes. To do so, we use bytearray() function by passing string and encoding as parameter values.

byte_arrayStr = "Tutorials Point bytearray"
str_byte_array = bytearray(byte_arrayStr, 'utf-8')
print("Creating bytearray from string:")
print(str_byte_array)

Following is an output of the above code −

Creating bytearray from string:
bytearray(b'Tutorials Point bytearray')
Example: Create bytearray of Specified Size Using bytearray()

The code below demonstrates how to create a byte array of the specified size. We are going to pass the size and value as arguments to the bytearray() function.

size = 5
value = 1
new_byte_array = bytearray([value]*size)
print("Bytearray of the given size:")
print(new_byte_array)

Output of the above code is as follows −

Bytearray of the given size:
bytearray(b'\x01\x01\x01\x01\x01')
Example: Modify bytearray

In the code below a bytearray is created using bytearray() function. Then we modify its 8th character using the ord() function. Since a bytearray object is mutable, it can be modified easily.

byte_arr = bytearray('Tuorialspoint', 'utf-8')
print("Original bytearray:", byte_arr)
byte_arr[8] = ord('P')
print("Modified bytearray:", byte_arr)

Following is an output of the above code −

Original bytearray: bytearray(b'Tuorialspoint')
Modified bytearray: bytearray(b'TuorialsPoint')

python_built_in_functions.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4