# Operations on bytes Objects

In this lesson, you’ll explore the common sequence operations that `bytes` objects support. You’ll take a closer look at:

• The `in` and `not in` operators
• Concatenation (`+`) and replication (`*`) operators
• Indexing and slicing
• Built-in functions `len()`, `min()`, and `max()`
• Methods for `bytes` objects
• `bytes.fromhex(<s>)` and `b.hex()`

Here’s how to use the `in` and `not in` operators:

Python
``````>>> a = b'abcde'
>>> a
b'abcde'

>>> b'cd' in a
True
>>> b'spam' in a
False
>>> b'spam' not in a
True
``````
Copied!

Here’s how to use the concatenation (`+`) and replication (`*`) operators:

Python
``````>>> a = b'abcde'
>>> a
b'abcde
>>> b = b'fghij'
>>> b
b'fghij'

>>> a + b
b'abcdefghij'

>>> a * 3
b'fghijfghijfghij'
``````
Copied!

Here’s how to do indexing and slicing:

Python
``````>>> a = b'abcde'
>>> a[2]
99
>>> a[1]
98
>>> a[2:4]
b'cd'
>>> a[1:5]
b'bcde'
``````
Copied!

Here’s how to use the built-in functions `len()`, `min()`, and `max()`:

Python
``````>>> a = b'abcde'
>>> len(a)
5
>>> max(a)
101
>>> chr(101)
'e'
>>> min(a)
97
>>> chr(97)
'a'
``````
Copied!

Here’s how to use the methods for `bytes` objects:

Python
``````>>> a = b'spam,egg,spam,bacon,spam,lobster'
>>> a
b'spam,egg,spam,bacon,spam,lobster'
>>> a.count(b'spam')
3
>>> a.count('spam')
Traceback (most recent call last):
File "<input>", line 1, in <module>
a.count('spam')
TypeError: argument should be integer or bytes-like object, not 'str'

>>> a.endswith(b'ster')
True

>>> a.find(b'bacon')
14

>>> a
b'spam,egg,spam,bacon,spam,lobster'
>>> a.split(sep=b',')
[b'spam', b'egg', b'spam', b'bacon' , b'spam', b'lobster']

>>> a.center(40, b'-')
b'----spam,egg,spam,bacon,spam,lobster----'

>>> a.center(40, ' ')
Traceback (most recent call last):
File "<input>", line 1, in <module>
a.center(40, ' ')
TypeError: center() argument 2 must be a byte string of length 1, not 'str'
``````
Copied!

Here’s how to use `bytes.fromhex(<s>)` and `b.hex()`:

Python
``````>>> a = b'spam,egg,spam,bacon,spam,lobster'
>>> a[1]
112
>>> a[3]
109
>>> hex(a[0])
'0x73'
>>> a[0]
115
>>> list(a)
[115, 112, 97, 109, 44, 101, 103, 103, 44, 115, 112, 97, 109, 44, 98, 97, 99, 111, 110, 44, 115, 112, 97, 109, 44, 108, 111, 98, 115, 116, 101, 114]

>>> b = bytes.fromhex(' aa 68 32 af ')
>>> b
b'\xaah2\xaf'
>>> list(b)
[170, 104, 50, 175]

>>> b
b'\xaah2\xaf'
>>> b.hex()
'aa6832af'
>>> type(b.hex())
<class 'str'>
``````
Copied!

Alain Rouleau

That `bytes.fromhex(' aa 68 32 af ')` was quite the head-scratcher! And as to why the output has both an `'h'` and a `'2'` in `b'\xaah2\xaf'`? You know, there’s no `'h'` in hexadecimal and why `'2'`?

But what appears to be happening is that hexadecimal 68 equals decimal 104 which in turn is ascii for `'h'`. Plus hexidecimal 32 is decimal 50 which in turn is ascii for `'2'`. All pretty crazy!

Veda

How does the console on VS Code look so colorful? Which plugin are you using?

Ricky White RP Team

I believe Chris uses BPython as his Python REPL. That is what you are seeing inside his VS Code Terminal

Veda

Thanks for your reply. I tried installing it on windows, but getting different dependency error while invoking it. (module fcntl not found ). I guess I have to stick with usual default REPL.

Veda

Got it fixed. On windows we need to use python -m bpython.cli instead of python to get to this CLI

Chris Bailey RP Team

Hi @Veda, I’m glad you found a solution! `bptyhon` on windows is a bit of work sometimes. I have found that it is often due to the support of some of the underlying pacakges (curses, etc).

In the future if you would like to look at some possilbe alternatives, with similar features: `ptpython` and `ipython`.

to join the conversation.