CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – bytes – A quick walk through with Examples

bytes is another sequence type in Python which holds sequence of bytes. Like strings, bytes also immutable; that means, once bytes objects are created; we are NOT ALLOWED to modify its’ content.

Unlike strings, where it holds sequence of Unicode characters; bytes are ALLOWED ONLY ASCII characters. Each character in a bytes object have the byte value of 0 to 255.

In this Article, I am going to quickly go through the introduction of bytes in Python and walk through with some working Examples.

Define a bytes object

We can define a bytes object using bytes literals (below statements) OR also using bytes class. bytes literals can be declared using single quotes (‘) or using double quotes (“). Remember that, bytes literals should be prefixed with the letter “b“; otherwise, Python treats them as a string literals.

>>> b1 = b'Bytes'
>>> b2 = b"Kilo Bytes"
>>> b3 = bytes(b"Mega Bytes")

Notice that, from above statement; while creating bytes object using bytes class constructor; if you miss the prefix “b” in the constructor; you will get below Error:

>>> b3 = bytes("Mega Bytes")
Traceback (most recent call last):
  File "", line 1, in 
TypeError: string argument without an encoding

This is because; you are passing a string literal to the bytes constructor; and there is no constructor defined in bytes class to accept string as an argument.

You have to be cautious while dealing with bytes and strings.

Like strings, we can add escape characters in bytes. Escape characters should start with backslash “\”. For example, “\n” for new line.

Like raw strings, we have raw bytes; where escape characters are not converted to special characters. Raw bytes should be prefixed with either “rb” or “br”. For example, below statements display raw bytes & bytes with escape characters.

>>> rb = rb"Raw\tBytes"
>>> rb
b'Raw\\tBytes'
>>> b4 = b"Bytes\t!"
>>> b4
b'Bytes\t!'
>>> 

Observe that, when we use “rb”; escape character “\t” is not converted and it is considered as “\\” & “t”; 2 different characters. But in case, with “b”; it considered “\t” as a single character.

Access elements from bytes

Like strings, you can access bytes elements using index operator. “0” is the start index and the index of last element (byte) is (number of elements – 1). Also it supports negative index, to access its’ elements from the end. “-1” is the start index from the end.

Below statements display first and last elements from the bytes object:

>>> b1[0]
66
>>> 
>>> b1[-1]
115
>>>

As I mentioned above, bytes holds sequence of ASCII characters. When you access its’ elements, it returns the ASCII value of the byte; instead of the character itself; because internally it stores each element as a byte.

Modify bytes elements

Python doesn’t allow to modify elements of immutable objects. When you attempt to modify bytes elements; Python interpreter displays below Error:

>>> b1[0] = b'b'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'bytes' object does not support item assignment

This is just a brief introduction about bytes. There is lot more to discuss; would like to cover most of the things through my upcoming Articles.

/Shijit/

Python – bytes – A quick walk through with Examples

One thought on “Python – bytes – A quick walk through with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top