![]() |
|
[Tut] Python Join List of Bytes (and What’s a Python Byte Anyway?) - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Join List of Bytes (and What’s a Python Byte Anyway?) (/thread-95599.html) |
[Tut] Python Join List of Bytes (and What’s a Python Byte Anyway?) - xSicKxBot - 06-10-2020 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 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. ![]() 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! |