Python bytearray() Function with Examples

bytearray() Function in Python:

The function bytearray() returns a bytearray object.

It can either convert objects to bytearray objects or create an empty bytearray object of the specified size.

The source parameter can be used to initialize the byte array in a variety of ways, including:

String: str. encode() is used to convert the string to bytes () Encoding and, optionally, errors must also be provided.

Integer: Creates an array of the specified size, with all elements set to null.

Object: The object’s read-only buffer will be used to initialize the byte array.

Iterable: Creates an array with the same size as the iterable count and initialized with the iterable elements. It must be iterable over integers between 0 <= x < 256

No source(arguments): Makes a 0-dimensional array.

Syntax:

bytearray(x, encoding, error)

Parameters

x: When creating the bytearray object, this is the source that will be used. If it is an integer, it will create an empty bytearray object of the specified size.

If it’s a String, make sure to specify the source’s encoding.

encoding: It is the string’s encoding

error: Specifies what should happen if the encoding fails.

Return Value: 

The bytearray() method returns a bytearray of the specified size and initialization values.

Examples:

Example1:

Input:

Given Number = 5

Output:

The bytearray object of the given number 5 = bytearray(b'\x00\x00\x00\x00\x00')

Example2:

Input:

Given String = "good morning btechgeeks"
# string with 'utf-8' encoding

Output:

The bytearray object of the given string =  bytearray(b'good morning btechgeeks')

bytearray() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the bytearray() method that returns a bytearray object of the given number.
  • Store it in another variable.
  • Print the bytearray object of the given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 5
# Pass the given number as an argument to the bytearray() method that returns
# a bytearray object of the given number.
# Store it in another variable.
rslt = bytearray(gvn_numb)
# Print the bytearray object of the given number. 
print("The bytearray object of the given number", gvn_numb, "=", rslt)

Output:

The bytearray object of the given number 5 = bytearray(b'\x00\x00\x00\x00\x00')
For Strings

Approach:

  • Give the string as static input and store it in a variable.
  • Pass the given string, ‘utf-8’ as the arguments to the bytearray() method that returns a bytearray object of the given string.
  • Store it in another variable.
  • Print the bytearray object of the given string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "good morning btechgeeks"
# Pass the given string, 'utf-8' as the arguments to the bytearray() method
# that returns a bytearray object of the given string.
# string with 'utf-8' encoding
# Store it in another variable.
rslt = bytearray(gvn_str, 'utf-8')
# Print the bytearray object of the given string. 
print("The bytearray object of the given string = ", rslt)

Output:

The bytearray object of the given string =  bytearray(b'good morning btechgeeks')
For Lists

Similarly, do the same for the list

gvn_lst = [1, 4, 7, 6]
rslt = bytearray(gvn_lst)
print("The bytearray object of the given gvn_lst = ", rslt)

Output:

The bytearray object of the given gvn_lst =  bytearray(b'\x01\x04\x07\x06')

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the bytearray() method that returns a bytearray object of the given number.
  • Store it in another variable.
  • Print the bytearray object of the given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the bytearray() method that returns
# a bytearray object of the given number.
# Store it in another variable.
rslt = bytearray(gvn_numb)
# Print the bytearray object of the given number. 
print("The bytearray object of the given number", gvn_numb, "=", rslt)

Output:

Enter some random number = 3
The bytearray object of the given number 3 = bytearray(b'\x00\x00\x00')
For Strings

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Pass the given string, ‘utf-8’ as the arguments to the bytearray() method that returns a bytearray object of the given string.
  • Store it in another variable.
  • Print the bytearray object of the given string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str =input("Enter some random string = ")
# Pass the given string, 'utf-8' as the arguments to the bytearray() method
# that returns a bytearray object of the given string.
# string with 'utf-8' encoding
# Store it in another variable.
rslt = bytearray(gvn_str, 'utf-8')
# Print the bytearray object of the given string. 
print("The bytearray object of the given string = ", rslt)

Output:

Enter some random string = hello all
The bytearray object of the given string = bytearray(b'hello all')