C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python bytearray() FunctionThe python bytearray() function returns a bytearray object and can convert objects into bytearray objects, or create an empty bytearray object of the specified size. Signaturebytearray(x, encoding, error) Parametersx (optional) : It is the source that initializes the array of bytes. encoding (optional) : It is an encoding of the string. error (optional) : It takes action when the encoding fails. ReturnIt returns an array of bytes. Python bytearray() Function Example 1The below example shows an array of bytes from a string: string = "Python is programming language." # string with encoding 'utf-8' arr = bytearray(string, 'utf-8') print(arr) Output: bytearray(b'Python is programming language.') Explanation: In the above example, we take a variable that contains a string value and convert it into a bytearray object. Python bytearray() Function Example 2The below example shows an array of bytes of given integer size: size = 5 arr = bytearray(size) print(arr) Output: bytearray(b'\x00\x00\x00\x00\x00') Python bytearray() Function Example 3The below example shows an array of bytes from an iterable list: rList = [2, 3, 4, 5, 6] arr = bytearray(rList) print(arr) Output: bytearray(b'\x02\x03\x04\x05\x06')
Next TopicPython Functions
|