Data Type - 'bytes' for Byte Sequence

This section describes the 'bytes' data type, which stores a sequence of bytes.

What Is the "bytes" Data Type? "bytes" is the byte sequence data type in Python. Each "bytes" object stores a sequence of bytes.

"bytes" data type has the following main Features.

1. "bytes" data objects can be created in several ways:

Here are some examples on how to create "bytes" objects:

>>> b'Say "Hi!"'
b'Say "Hi!"'

>>> b"Newton's laws"
b"Newton's laws"

# any special or non-ASCII character can be escaped
>>> b'Say\x0aHi!'
b'Say\nHi!'

>>> bytes('Say "Hi!"', 'utf-8')
b'Say "Hi!"'

>>> bytes('Say "Hi!"', 'utf-16')
b'\xff\xfeS\x00a\x00y\x00 \x00"\x00H\x00i\x00!\x00"\x00'

2. "bytes" data type provides an array-style expression to access the byte of a given position.

>>> x = b'apple'
>>> type(x)
<class 'bytes'>

>>> b = x[0]
>>> b
97
>>> type(b)
<class 'int'>

>>> b.to_bytes(1, 'big')
b'a'

3. "bytes" data type is an immutable data type. Once a "bytes" data object is created to store a byte sequence, this sequence will never change.

>>> x = b'apple'
>>> b = x[0]
>>> b
97

>>> x[0] = 98
TypeError: 'bytes' object does not support item assignment

Don't get confused about "bytes" immutability and "bytes" variable re-assignment capability. A "bytes" variable can be re-assigned with different "bytes" objects many times.

# new object is created for b'apple' and assigned to x
>>> x = b'apple'
>>> id(x)
4552932784

# assign the same object to y
>>> y = x
>>> id(y)
4552932784

# new object is created for b'orange' and assigned to x
>>> x = b'orange'
>>> id(x)
4552933024

# the first object for b'apple' is still there in memory
>>> y
b'apple'
>>> id(y)
4552932784

3. Because "bytes" data type is immutable, an object created for a given byte sequence is not allowed to change its value. This allow "bytes" objects to be cached in memory and reused later whenever they are needed again. Reusing objects will reduce execution time and memory consumption.

However on my macOS computer, "bytes" objects are not cached and reused at all. The code example below shows you that there are 4 "bytes" objects created for 4 requests of b'apple'.

>>> x = b'apple'
>>> y = b'apple'
>>> (id(x), id(y), id(b'apple'), id(bytes("apple", 'ascii')))
(4552932640, 4552931776, 4552971824, 4552237296)

5. "bytes" objects support the following operations:

    Syntax   Operation       Note
----------   -------------   ------------------
     x + y   Concatenation
     x * n   Repetition      n is an "int"
     n * x   Repetition      Same as x * n
    s in x   Look up         s is a sub "bytes"
s not in x   Look up         Same as !(s in x)

6. "bytes" objects support the following comparison operations. They are performed by compare the byte value from each position starting from the left side.

Syntax   Operation
------   ---------
x <  y   Less than
x <= y   Less than or equal
x >  y   Greater than
x >= y   Greater than or equal
x == y   Equal
x != y   Not equal

7. Some built-in functions are provided for "bytes" objects.

8. Some instance methods are provided for "bytes" objects.

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

Built-in Data Types

 Introduction to Data Type

 Common Features of All Data Types

 Data Type - NoneType for Nothing

 Data Type - 'bool' for Boolean Values

 Data Type - 'int' for Integer Values

 Data Type - 'float' for Real Numbers

Data Type - 'bytes' for Byte Sequence

 Data Type - 'str' for Character String

 Data Type - 'tuple' for Immutable List

 Data Type - 'list' for Mutable List

 Data Type - 'dict' for Dictionary Table

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB