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
b'...' similar to the string notation
'...'. To join a list of Bytes, call the
Byte.join(list) method. If you try to join a list of Bytes on a string delimiter, Python will throw a
TypeError, so make sure to call it on a Byte object
b' '.join(...) 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:
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.
Let’s keep that on a hold for a moment and look how you create a simple byte object:
>>> 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
'X'. The ASCII code of character
'X' is 88, or in binary format
0b1011000. 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:
>>> a = b'hello' >>> a b'hello' >>> type(a) <class 'bytes'>
Again, you create the Byte object for
'hello'. You cannot encode the whole string
'hello' in eight bits (=byte), so the Python Byte object
a 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
b'...' instead of
'...'. As string objects, Bytes are immutable—so you cannot modify them after creation.
Concatenate Byte Strings in Python
You can concatenate two byte objects
y in Python by using the (overloaded) add operation
>>> 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
Byte.join(iterable) 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.
|A collection of Byte objects.|
Examples: Let’s see a bunch of examples on how you can join a collection of Byte objects!
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
join() 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
join() method! Say, you’ve got an iterable of Bytes to be joined. If you call the
string.join(...) method instead of the
Byte.join(...) method, Python will throw a TypeError with the following error message:
lst = [b'Python', b'is', b'beautiful'] print(' '.join(lst)) # TypeError: sequence item 0: expected str instance, bytes found
The error message doesn’t directly tell you how to fix this issue. The reason is that you call the
join() method on the string object. And the string
join() 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
join(...) method on the Byte object
b' '.join(...) instead of
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
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.