06-10-2020, 02:38 AM
Python Join List of Bytes (and What’s a Python Byte Anyway?)
When teaching the Finxter Python students, I’m often shocked how even intermediate-level coders do not understand the very basic foundations of computer science. In CS, there’s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called bits).
This tutorial aims to clarify some misconceptions and questions regarding Bytes in Python—a term that has a quite different meaning to bytes in general. In particular, you’ll learn the definition of the Byte object and how to correctly join a sequence of Bytes. Here’s the short answer:
A Python Byte is a sequence of bytes. You create a Byte object using the notation similar to the string notation . To join a list of Bytes, call the method. If you try to join a list of Bytes on a string delimiter, Python will throw a , so make sure to call it on a Byte object rather than .
Before you dive into all of this in a step-by-step manner, let’s play a bit with the interactive code shell to open your knowledge gap:
https://trinket.io/embed/python/ff165cf3ab
Exercise: What happens if you join the list of Bytes on a string delimiter? Try it in the shell!
What’s a Byte in Python Anyway?
You think you know what a byte is, right? It’s a sequence of 8 bits.
Well, that’s what a byte is outside the Python world. In Python, it’s not the whole story.
Python comes with a built-in Byte data type. According to the official documentation, the Byte object is an “immutable sequence of bytes“. If you talk about a byte outside Python, you mean 8 bits. If you talk about a byte inside Python, you mean a sequence of one or more bytes.
![[Image: byte-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/byte-1024x576.jpg)
Let’s keep that on a hold for a moment and look how you create a simple byte object:
You create the Byte object just like you create a string in Python using the single, double, or triple quotes around a text that’s to be encoded in a “sequence of bytes”. In this case, the sequence of bytes consists only of the bytes needed to encode a single character. The ASCII code of character is 88, or in binary format . You can see that you only need one byte to encode the character.
But can you also create more complicated “Python Byte” objects that consist of more “real bytes”? Sure, you can:
Again, you create the Byte object for. You cannot encode the whole string in eight bits (=byte), so the Python Byte object now consists of a “sequence of bytes”.
Takeaway: A (Python) Byte is a sequence of bytes and you can use it just like you use strings by using the syntax instead of . As string objects, Bytes are immutable—so you cannot modify them after creation.
Concatenate Byte Strings in Python
You can concatenatetwo byte objects and in Python by using the (overloaded) add operation :
This works for Python 2 and Python 3—and it will probably also work in Python 4! (Why? Because the semantics of the __add__operation won’t change just by setting up a new programming language or modifying an old one.)
How to Join a List of Bytes?
Python Bytes are similar than Python strings (at least for you, the person who used the Byte data structure in their program).
So, joining a list of Bytes is similar than joining a list of strings: use the join method! The Byte object comes with a method that concatenates all Byte objects in the .
Keep in mind that a Byte object is a sequence of bytes by itself (and not a sequence of bits as you may have expected). And, yes, I’ll repeat this until it sticks.
Syntax:
Argument |
Description
|
A collection of Byte objects.
Examples: Let’s see a bunch of examples on how you can join a collectionof Byte objects!
You get the point: you call the method on the Byte object and pass an iterable of Byte objects to be concatenated. Notice how all involved data types are Byte objects!
How to Fix “TypeError: sequence item 0: expected str instance, bytes found”?
A common mistake of many Python coders is to call the wrong method! Say, you’ve got an iterable of Bytes to be joined. If you call the method instead of the method, Python will throw a TypeErrorwith the following error message:
The error message doesn’t directly tell you how to fix this issue. The reason is that you call the method on the string object. And the string method expects you to pass an iterable of string objects to be concatenated. But you pass an iterable of Byte objects!
To fix it, simply call the method on the Byte object instead of .
This way, you can easily concatenate multiple Byte objects by using the method.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
When teaching the Finxter Python students, I’m often shocked how even intermediate-level coders do not understand the very basic foundations of computer science. In CS, there’s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called bits).
This tutorial aims to clarify some misconceptions and questions regarding Bytes in Python—a term that has a quite different meaning to bytes in general. In particular, you’ll learn the definition of the Byte object and how to correctly join a sequence of Bytes. Here’s the short answer:
A Python Byte is a sequence of bytes. You create a Byte object using the notation
Code:
b'...'Code:
'...'Code:
Byte.join(list)Code:
TypeErrorCode:
b' '.join(...)Code:
' '.join(...)Before you dive into all of this in a step-by-step manner, let’s play a bit with the interactive code shell to open your knowledge gap:
https://trinket.io/embed/python/ff165cf3ab
Exercise: What happens if you join the list of Bytes on a string delimiter? Try it in the shell!
What’s a Byte in Python Anyway?
You think you know what a byte is, right? It’s a sequence of 8 bits.
Well, that’s what a byte is outside the Python world. In Python, it’s not the whole story.
Python comes with a built-in Byte data type. According to the official documentation, the Byte object is an “immutable sequence of bytes“. If you talk about a byte outside Python, you mean 8 bits. If you talk about a byte inside Python, you mean a sequence of one or more bytes.
![[Image: byte-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/byte-1024x576.jpg)
Let’s keep that on a hold for a moment and look how you create a simple byte object:
Code:
>>> a = b'X'
>>> a
b'X'
>>> type(a)
<class 'bytes'>You create the Byte object just like you create a string in Python using the single, double, or triple quotes around a text that’s to be encoded in a “sequence of bytes”. In this case, the sequence of bytes consists only of the bytes needed to encode a single character
Code:
'X'Code:
'X'Code:
0b1011000But can you also create more complicated “Python Byte” objects that consist of more “real bytes”? Sure, you can:
Code:
>>> a = b'hello'
>>> a
b'hello'
>>> type(a)
<class 'bytes'>Again, you create the Byte object for
Code:
'hello'Code:
'hello'Code:
aTakeaway: A (Python) Byte is a sequence of bytes and you can use it just like you use strings by using the syntax
Code:
b'...'Code:
'...'Concatenate Byte Strings in Python
You can concatenatetwo byte objects
Code:
xCode:
yCode:
x+yCode:
>>> x = b'hello '
>>> y = b'world!'
>>> x + y
b'hello world!'This works for Python 2 and Python 3—and it will probably also work in Python 4! (Why? Because the semantics of the __add__operation won’t change just by setting up a new programming language or modifying an old one.)
How to Join a List of Bytes?
Python Bytes are similar than Python strings (at least for you, the person who used the Byte data structure in their program).
So, joining a list of Bytes is similar than joining a list of strings: use the join method! The Byte object comes with a method
Code:
Byte.join(iterable)Code:
iterableKeep in mind that a Byte object is a sequence of bytes by itself (and not a sequence of bits as you may have expected). And, yes, I’ll repeat this until it sticks.
Syntax:
Code:
Byte.join(iterable)Argument |
Description
Code:
iterableA collection of Byte objects.
Examples: Let’s see a bunch of examples on how you can join a collectionof Byte objects!
Code:
lst = [b'Python', b'is', b'beautiful'] # Example 1
print(b' '.join(lst))
b'Python is beautiful' # Example 2
print(b'-'.join(lst))
b'Python-is-beautiful' # Example 3
print(b'\n'.join(lst))
b'Python\nis\nbeautiful'You get the point: you call the
Code:
join()How to Fix “TypeError: sequence item 0: expected str instance, bytes found”?
A common mistake of many Python coders is to call the wrong
Code:
join()Code:
string.join(...)Code:
Byte.join(...)Code:
lst = [b'Python', b'is', b'beautiful']
print(' '.join(lst))
# TypeError: sequence item 0: expected str instance, bytes foundThe error message doesn’t directly tell you how to fix this issue. The reason is that you call the
Code:
join()Code:
join()To fix it, simply call the
Code:
join(...)Code:
b' '.join(...)Code:
' '.join(...)Code:
lst = [b'Python', b'is', b'beautiful']
print(b' '.join(lst))
b'Python is beautiful'This way, you can easily concatenate multiple Byte objects by using the
Code:
Byte.join()Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!

