- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- Python Versions
- Python Environment Setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control & Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break Vs continue
- Python pass Vs continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List Vs Tuple Vs Dict Vs Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python Send E-mail
- Python Event Handling
- Python Keywords
- Python All Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List
bytearray in Python
bytearray in Python is a binary sequence type. There are 3 binary sequence types available in Python:
The bytearray is the core inbuilt type used in manipulating the binary data. Unlike bytes, bytearray objects are mutable or changeable.
Creating bytearray in Python
A bytearray object can be created in four ways. All those fours ways of creating a bytearray object in Python are given below.
Using bytearray() Constructor
The bytearray() constructor is always required to create a bytearray object in Python. Here is an example:
x = bytearray() print(x) print(type(x))
The snapshot given below shows the sample output produced by this Python program, showing the value and type of variable x:
Creating bytearray of Given Length
Here is another example of creating a bytearray object with specified length:
x = bytearray(5) print(x) print(type(x))
Following is the output produced by this Python code:
bytearray(b'\x00\x00\x00\x00\x00') <class 'bytearray'>
Creating bytearray from an Iterable
Here is the third example of creating a bytearray from a iterable:
x = bytearray(range(5)) print(x) print(type(x))
The output would be:
bytearray(b'\x00\x01\x02\x03\x04') <class 'bytearray'>
Copying an Existing Binary Data via Buffer Protocol
Here is the last example of creating a bytearray object by copying an existing binary data via the buffer protocol:
x = bytearray(b'codescracker dot com') print(x) print(type(x))
The output would be:
bytearray(b'codescracker dot com') <class 'bytearray'>
Python bytearray Example
This program is created to allow user to enter a string. The entered string gets converted into bytearray object using the bytearray() and with the utf-8 and utf-16 encoding. You can try some other encoding too, with yourself.
print("Enter the String: ", end="") str = input() print("\nString converted into bytearray object with UTF-8 encoding:") ba = bytearray(str, "UTF-8") print(ba) print("\nString converted into bytearray object with UTF-16 encoding:") ba = bytearray(str, "UTF-16") print(ba)
The snapshot given below shows the sample run with user input python:
Python bytearray Objects are Mutable
Because bytearray objects are mutable, therefore we can modify the bytes object anytime in the same program. Here is an example:
str = "codescracker dot com" x = bytearray(str, "utf-8") print(x) del x[12:] print(x) x[12:] = b".com" print(x)
In above program, using the statement:
del x[12:]
all characters from index number 12th (included) to last, will get deleted. And using the following statement:
x[12:] = b".com"
the new value, .com will get appended/replaced from the 12th index. Here is the sample output produced by above Python program:
bytearray(b'codescracker dot com') bytearray(b'codescracker') bytearray(b'codescracker.com')
Note - To learn about slicing in detail, refer to Python List. There, you'll get all the detail.
To convert bytearray object to a string object, refer to Python bytearray to string program article.
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube