Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Join List of Bytes (and What’s a Python Byte Anyway?)

#1
Python Join List of Bytes (and What’s a Python Byte Anyway?)

<div><figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python Join List of Bytes (and What’s a Python Byte Anyway?)" width="1400" height="788" src="https://www.youtube.com/embed/ub6N-ixoApg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>When teaching the <a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe/" target="_blank">Finxter Python students</a>, I’m often shocked how even intermediate-level coders do not understand the very basic foundations of <a href="https://blog.finxter.com/what-is-a-career-in-computer-programming-like/" target="_blank" rel="noreferrer noopener">computer science</a>. In CS, there’s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called <em>bits</em>). </p>
<p>This tutorial aims to clarify some misconceptions and questions regarding <strong><em>Bytes in Python</em></strong>—a term that has a quite different meaning to <strong><em>bytes in general</em></strong>. 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:</p>
<p class="has-background has-luminous-vivid-amber-background-color">A Python Byte is a sequence of bytes. You create a Byte object using the notation <code>b'...'</code> similar to the string notation <code>'...'</code>. To join a list of Bytes, call the <code>Byte.join(list)</code> method. If you try to join a list of Bytes on a <em>string </em>delimiter, Python will throw a <code>TypeError</code>, so make sure to call it on a Byte object <code>b' '.join(...)</code> rather than <code>' '.join(...)</code>. </p>
<p>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:</p>
<p> <iframe src="https://trinket.io/embed/python/ff165cf3ab" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: What happens if you join the list of Bytes on a string delimiter? Try it in the shell!</em></p>
<h2>What’s a Byte in Python Anyway?</h2>
<p>You think you know what a byte is, right? It’s a sequence of 8 bits.</p>
<p>Well, that’s what a byte is outside the Python world. In Python, it’s not the whole story.</p>
<p>Python comes with a built-in Byte data type. According to the <a href="https://docs.python.org/3/library/stdtypes.html#bytes-objects" target="_blank" rel="noreferrer noopener">official documentation</a>, the Byte object is an “<em>immutable sequence of bytes</em>“. If you talk about a byte outside Python, you mean 8 bits. If you talk about a byte inside Python, you mean a <em><strong>sequence of one or more bytes. </strong></em></p>
<figure class="wp-block-image size-large"><img src="https://blog.finxter.com/wp-content/uploads/2020/06/byte-1024x576.jpg" alt="" class="wp-image-9296" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/byte-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>Let’s keep that on a hold for a moment and look how you create a simple byte <a href="https://blog.finxter.com/object-oriented-programming-terminology-cheat-sheet/" target="_blank" rel="noreferrer noopener">object</a>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> a = b'X'
>>> a
b'X'
>>> type(a)
&lt;class 'bytes'></pre>
<p>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>. The <a href="https://theasciicode.com.ar/ascii-printable-characters/capital-letter-x-uppercase-ascii-code-88.html" target="_blank" rel="noreferrer noopener">ASCII code</a> of character <code>'X'</code> is 88, or in binary format <code>0b1011000</code>. You can see that you only need one byte to encode the character.</p>
<p><strong>But can you also create more complicated “Python Byte” objects that consist of more “real bytes”?</strong> Sure, you can:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> a = b'hello'
>>> a
b'hello'
>>> type(a)
&lt;class 'bytes'></pre>
<p>Again, you create the Byte object for <code>'hello'</code>. You cannot encode the whole string <code>'hello'</code> in eight bits (=byte), so the Python Byte object <code>a</code> now consists of a “sequence of bytes”. </p>
<p><strong>Takeaway</strong>: A (Python) Byte is a <em><strong>sequence</strong></em> of bytes and you can use it just like you use strings by using the syntax <code>b'...'</code> instead of <code>'...'</code>. As string objects, Bytes are immutable—so you cannot modify them after creation.</p>
<h2>Concatenate Byte Strings in Python</h2>
<p>You can <a href="https://blog.finxter.com/concatenate-lists-in-python/" target="_blank" rel="noreferrer noopener">concatenate </a>two byte objects <code>x</code> and <code>y</code> in Python by using the (overloaded) add operation <code>x+y</code>: </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> x = b'hello '
>>> y = b'world!'
>>> x + y
b'hello world!'</pre>
<p>This works for Python 2 and Python 3—and it will probably also work in Python 4! <em>(Why? Because the semantics of the <a href="https://blog.finxter.com/python-list-concatenation-add-vs-inplace-add-vs-extend/" target="_blank" rel="noreferrer noopener">__add__ </a>operation won’t change just by setting up a new programming language or modifying an old one.)</em></p>
<h2>How to Join a List of Bytes?</h2>
<p>Python Bytes are similar than <a href="https://blog.finxter.com/python-trim-string/" target="_blank" rel="noreferrer noopener">Python strings</a> (at least for you, the person who used the Byte data structure in their program).</p>
<p>So, joining a list of Bytes is similar than <a rel="noreferrer noopener" href="https://blog.finxter.com/python-join-list/" target="_blank">joining a list of strings</a>: use the join method! The Byte object comes with a method <code>Byte.join(iterable)</code> that concatenates all Byte objects in the <code>iterable</code>. </p>
<p><em>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.</em></p>
<p><strong>Syntax</strong>: <code>Byte.join(iterable)</code></p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>iterable</code></td>
<td>A collection of Byte objects.</td>
</tr>
</tbody>
</table>
</figure>
<p><strong>Examples</strong>: Let’s see a bunch of examples on how you can join a <a href="https://blog.finxter.com/python-join-list/" target="_blank" rel="noreferrer noopener">collection </a>of Byte objects!</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
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'</pre>
<p>You get the point: you call the <code>join()</code> method on the Byte object and pass an iterable of Byte objects to be concatenated. Notice how all involved data types are Byte objects!</p>
<h2>How to Fix “TypeError: sequence item 0: expected str instance, bytes found”?</h2>
<p>A common mistake of many Python coders is to call the wrong <code>join()</code> method! Say, you’ve got an iterable of Bytes to be joined. If you call the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-join-list/" target="_blank"><code>string.join(...)</code> method</a> instead of the <code>Byte.join(...)</code> method, Python will throw a <a href="https://blog.finxter.com/convert-list-of-tuples-to-string/" target="_blank" rel="noreferrer noopener">TypeError </a>with the following error message:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [b'Python', b'is', b'beautiful']
print(' '.join(lst))
# TypeError: sequence item 0: expected str instance, bytes found</pre>
<p>The error message doesn’t directly tell you how to fix this issue. The reason is that you call the <code>join()</code> method on the string object. And the string <code>join()</code> method expects you to pass an iterable of string objects to be concatenated. But you pass an iterable of Byte objects!</p>
<p>To fix it, simply call the <code>join(...)</code> method on the Byte object <code>b' '.join(...)</code> instead of <code>' '.join(...)</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [b'Python', b'is', b'beautiful']
print(b' '.join(lst))
b'Python is beautiful'</pre>
<p>This way, you can easily concatenate multiple Byte objects by using the <code>Byte.join()</code> method. </p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/06/...te-anyway/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016