TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

bytearray() in Python | Python bytearray() Function with Examples

bytearray() in Python | Python bytearray() Function with Examples with Examples on Python, Built in, Functions, abs Function, all Function, bin Function, bool Function, python Functions, new sum Function, bytes Function, new callable Function etc.

<< Back to PYTHON

Python bytearray() Function

The python bytearray() function returns a bytearray object and can convert objects into bytearray objects, or create an empty bytearray object of the specified size.

Signature

bytearray(x, encoding, error)

Parameters

x (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.

Return

It returns an array of bytes.

Python bytearray() Function Example 1

The 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 2

The 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 3

The 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf