Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python bytearray() function

  • Safa MulaniSafa Mulani
  • March 29, 2020
  • Built-in Methods
Python bytearray() function

In this article, we will be having a look at one of the Python’s in-built function — Python bytearray() function.

Understanding Python bytearray() function

Python has in-built bytearray() method that creates an array of bytes and returns a bytearray object of the created array of the defined size by a particular iterable or a value.

Syntax:

bytearray(source_input, encoding_scheme, error)

Parameters:

  • source_input: It is an optional parameter. It is basically used to initialize the array data elements. The source_input can be an iterable, value, etc.
  • encoding_scheme(optional): It is used to define the encoding pattern for a string.
  • error(optional): It defines the actions that need to be taken in case the encoding fails.

The bytearray() method takes an iterable such as list, string, tuple, etc or value as an argument and initializes an array with the size and returns a bytearray object of it.

Example: Python bytearray() function with no arguments

inp = bytearray()
print(inp)

When no argument is passed to the bytearray() function, the function returns an empty bytearray object.

Output:

bytearray(b'')

1. Python bytearray() function with string as an argument

When a string value is passed as an argument to the bytearray() function, it converts the string into an array of bytes.

The mandatory condition continues to stay such that whenever a string is passed in the parameter list, encoding has to be defined for the same in the parameter list, otherwise a TypeError exception is raised.

Example 1:

inp_str = "JournalDev"

arr_8 = bytearray(inp_str, 'utf-8') 

print(arr_8) 

Here, we have passed the encoding_scheme as ‘utf-8‘ for the input string to be converted to an array of bytes.

Output:

bytearray(b'JournalDev')

Example 2:

inp_str = "JournalDev"

arr_16 = bytearray(inp_str, 'utf-16') 

print(arr_16) 

In the above example, the encoding scheme is defined as ‘utf-16‘.

Output:

bytearray(b'\xff\xfeJ\x00o\x00u\x00r\x00n\x00a\x00l\x00D\x00e\x00v\x00')

2. Python bytearray() function with iterable passed as a parameter

When an iterable such as list, set, tuple, etc is passed as an argument to the bytearray() function, it returns an array of bytes that contains the initial contents as the array elements in the bytearray object.

Mandatory condition: If an iterable is passed as an argument to the bytearray() function, it is necessary for all the elements of the iterable to be of type integer to avoid TypeError.

Example: Python bytearray() with list

inp_lst = [2, 4, 6, 8]
res_byte = bytearray(inp_lst)
print(res_byte)

As clearly understood, the contents of the list i.e. [2,4,6,8] have been used to create a bytearray object.

Output:

bytearray(b'\x02\x04\x06\x08')

3. Python bytearray() function with integer value as an argument

If the bytearray() function encounters an integer value as a parameter, it then creates a bytearray object with the size = integer value and initializes it with null values (‘\0’).

Example:

inp = 14
res_byte = bytearray(inp)
print(res_byte)

In the above example, a array object is created with the array size as ’14’ and initialized with null values.

Output:

bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

Summary

  • Python bytearray() function returns a bytearray object i.e. generates an array of bytes of the type of source input provided to the function.
  • The bytearray() function can have an iterable, values, etc as parameters to it.
  • Whenever an iterable is passed the function, it is essential for the elements of the iterable to be of integer type only.

Conclusion

Thus, in this article, we have understood the working of the Python bytearray() method with various types of parameters.


References

  • Python bytearray() – JournalDev
  • Python bytearray() function – Official Documentation
Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
How to use the Python divmod() function
Previous Post How to use the Python divmod() function
Next Post Append to a List in Python
Append to a List in Python

Related Posts

Print Function Python: Arguments and Output

Print Function Python: Arguments and Output

  • November 28, 2026
How to Use Input Function in Python

How to Use Input Function in Python

  • November 28, 2026
How to Open Files in Python

How to Open Files in Python

  • November 23, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.