Python: bytearray() function
bytearray() function
The bytearray() function is used to get a bytearray object.
Syntax:
bytearray([source[, encoding[, errors]]])
Version:
(Python 3)
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.
Return value
Return a new array of bytes.
Example: Array of bytes from an iterable list
num = [1, 2, 3, 4, 5]
a = bytearray(num)
print(a)
Output:
bytearray(b'\x01\x02\x03\x04\x05')
Pictorial Presentation:
Example: Array of bytes from a string
str = "Python bytearray example."
# string with encoding 'utf-8'
a = bytearray(str, 'utf-8')
print(a)
Output:
bytearray(b'Python bytearray example.')
Pictorial Presentation:
Example: Array of bytes of given integer size:
size = 10
a = bytearray(size)
print(a)
Output:
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
Python Code Editor:
Previous: bool()
Next: bytes()
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework