A computer stores data in its memory in binary (0's and 1's) format only. A single bit (binary digit) is the smallest possible unit of data in a computer. Typically a group of eight bits is known as a byte. Eg 10100101 this is a byte. A single byte represents numbers between 0 (00000000) to 255 (11111111), so 256 (2^8) bits in total, similarly a KiloByte (KB) is 1024 bytes, a MegaBytes (MB) is 1024 KB and so on. A file is a sequence of these bytes, the size of a file is determined by the number of bytes in them. A programming language usually deals with two types of file, a Text file and Binary file. Text file contains character data and Binary files contain well.. binary data. Images, documents, executables & compressed files, compiled programs etc are examples of binary files. Now to store characters (Text files) in a computer, they need to be encoded as binary data. For this encoding schemes are used. It is simply a way to represent Character data (human readable) in Binary format (computer readable), schemes like ASCII, UTF-8 or UTF-16 are used. A Encoding scheme has to follow a Character Set (such as ASCII/Unicode), which is basically a table of unique numbers assigned to the letters, numbers and symbols used in languages or on keyboards. This way using the encoding schemes the text data is converted to binary (bytes) data and then stored in a computer storage.
Parameters:
str
: Provide encoding scheme for the source string.str
: Way to handle errors for source data.Explanation: This function creates an immutable object consisting of Unicode (character set containing all major languages characters) 0-256 characters.
## Create a bytes object
data = bytes("This is bytes data using ascii encoding.", 'ascii')
print(data) # b'This is bytes data using ascii encoding.'
data = bytes("This is bytes data using utf-8 encoding.", 'UTF-8')
print(data) # b'This is bytes data using utf-8 encoding.'
# or can use b prefix on string like syntax
print((b'42')) # b'42'
print(type(b'42')) # <class 'bytes'>
## Indexing a bytes object returns a Unicode of a character,
# which we can use to convert it back to a character
print(data[0], chr(data[0])) # 84 T
## Create bytes object using a iterable objects
# from a list
print(bytes([1,2,3])) # b''
# from a tuple
print(bytes((80,50,60))) # b'P2<'
Parameters:
str
: Provide encoding scheme, if source is string.str
: Way to handle errors, if the source is a string.Explanation: This function returns a mutable version of bytes object.
bytearray
type.## Create bytearray with 0's by providing size in int
output = bytearray(4)
print(output) # bytearray(b'