Python bytes

Python bytes

Python bytes object is a sequence of single bytes.

Python bytes object is immutable, so inplace update operations or modifications on the original bytes object cannot be done.

Initiate a Python bytes object

You can define a bytes object using single quotes, double quotes or triple coated; with literal b prefixed.

bytes with single quotes

bytesObj = b'52s3a6s1s2xA$as2'

When using single quotes to define, you can include double quote character in the bytes sequence. But if you would like include a single quote character, use escape character \ back slash.

bytesObj = b'52s3a6"s1s2xA$as2'
bytesObj = b'52s3a6s1s\'2xA$as2'  #use escape character for single quote inside single quotes

bytes with double quotes

bytesObj = b"52s3a6s1s2xA$as2"

Similar to the case with single quotes, when using double quotes to define a bytes object, you can include single quote character in the bytes sequence as is. But if you would like include a double quote character, use escape character \ back slash.

bytesObj = b"52s3a6s1s2'xA$as2"
bytesObj = b"52s3a6\"s1s2xA$as2" #use escape character for double quote inside double quotes

bytes with triple quotes

bytesObj = b'''52s3a6s1s2xA$as2'''
bytesObj = b"""52s3a6s1s2xA$as2"""

To include triple quotes inside triple quotes is no exception. You have escape the first of the triple quote inside bytes sequence.

bytesObj = b'''52s3a6\'''s1s2xA$as2'''
bytesObj = b"""52s3a6\"""s1s2xA$as2"""

Python bytes with hexadecimal characters

You can include hexadecimal characters in the bytes sequence. You have to use \x prefix for each hexadecimal character you place in the bytes object.

bytesObj = b'52s3a6s\xFD1s2xA$as2'

In the above object, \xFD is a single byte in hexadecimal format.

Iterate over Python Bytes Sequence

Python bytes is a sequence. Therefore, we can use iterate over it using a looping technique.

In the following example, we will use for loop to iterate over the bytes of the Python bytes object.

Python Program

bytesObj = b'52s3a6'

for byte in bytesObj:
    print(byte)
Run Code Copy

Output

53
50
115
51
97
54

The byte values are the integer representations. So, we are seeing numbers in the output for each byte.

Conversions

You can convert a bytes object to other datatypes or other datatype objects to bytes object.

For example, in the following program, we shall convert a bytes object to string and string object to bytes.

Python Program

# Bytes to string
bytesObj = b'52s3a6s1s2xA$as2'
strObj = bytesObj.decode("utf-8") 
print(strObj)

# String to bytes
strObj = 'hello'
bytesObj = bytes(strObj, "utf-8")
print(bytesObj)
Run Code Copy

Output

52s3a6s1s2xA$as2
b'hello'

In the first part of the code, we have taken a bytes object and converted it to string using decode() method.

In the second part of the code, we have taken a string and converted it to bytes object using bytes() builtin function.

Similar to this, we have a lot of conversions from and to bytes type, and following is the list of tutorials for converting bytes to and fro from other types.

  • Convert Bytes to String
  • Convert String to Bytes
  • Convert Bytes to Int

Summary

In this tutorial of Python Examples, we have learned what a bytes object is in Python, how to initialize a bytes object, how to make conversions to and fro with other data types, etc.

Related Tutorials

Code copied to clipboard successfully 👍