{"id":113845,"date":"2020-06-06T08:49:42","date_gmt":"2020-06-06T08:49:42","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9280"},"modified":"2020-06-06T08:49:42","modified_gmt":"2020-06-06T08:49:42","slug":"python-join-list-of-bytes-and-whats-a-python-byte-anyway","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/06\/python-join-list-of-bytes-and-whats-a-python-byte-anyway\/","title":{"rendered":"Python Join List of Bytes (and What\u2019s a Python Byte Anyway?)"},"content":{"rendered":"<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python Join List of Bytes (and What\u2019s 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>\n<\/div>\n<\/figure>\n<p>When teaching the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/subscribe\/\" target=\"_blank\">Finxter Python students<\/a>, I&#8217;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&#8217;s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called <em>bits<\/em>). <\/p>\n<p>This tutorial aims to clarify some misconceptions and questions regarding <strong><em>Bytes in Python<\/em><\/strong>&#8212;a term that has a quite different meaning to <strong><em>bytes in general<\/em><\/strong>. In particular, you&#8217;ll learn the definition of the Byte object and how to correctly join a sequence of Bytes. Here&#8217;s the short answer:<\/p>\n<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>\n<p>Before you dive into all of this in a step-by-step manner, let&#8217;s play a bit with the interactive code shell to open your knowledge gap:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/ff165cf3ab\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<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>\n<h2>What&#8217;s a Byte in Python Anyway?<\/h2>\n<p>You think you know what a byte is, right? It&#8217;s a sequence of 8 bits.<\/p>\n<p>Well, that&#8217;s what a byte is outside the Python world. In Python, it&#8217;s not the whole story.<\/p>\n<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 &#8220;<em>immutable sequence of bytes<\/em>&#8220;. 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>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" 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\/uploads\/2020\/06\/byte-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/byte-768x432.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>Let&#8217;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>\n<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'\n>>> a\nb'X'\n>>> type(a)\n&lt;class 'bytes'><\/pre>\n<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&#8217;s to be encoded in a &#8220;sequence of bytes&#8221;. 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>\n<p><strong>But can you also create more complicated &#8220;Python Byte&#8221; objects that consist of more &#8220;real bytes&#8221;?<\/strong> Sure, you can:<\/p>\n<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'\n>>> a\nb'hello'\n>>> type(a)\n&lt;class 'bytes'><\/pre>\n<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 &#8220;sequence of bytes&#8221;. <\/p>\n<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&#8212;so you cannot modify them after creation.<\/p>\n<h2>Concatenate Byte Strings in Python<\/h2>\n<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>\n<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 '\n>>> y = b'world!'\n>>> x + y\nb'hello world!'<\/pre>\n<p>This works for Python 2 and Python 3&#8212;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&#8217;t change just by setting up a new programming language or modifying an old one.)<\/em><\/p>\n<h2>How to Join a List of Bytes?<\/h2>\n<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>\n<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>\n<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&#8217;ll repeat this until it sticks.<\/em><\/p>\n<p><strong>Syntax<\/strong>: <code>Byte.join(iterable)<\/code><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Argument<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>iterable<\/code><\/td>\n<td>A collection of Byte objects.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><strong>Examples<\/strong>: Let&#8217;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>\n<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=\"\">\nlst = [b'Python', b'is', b'beautiful'] # Example 1\nprint(b' '.join(lst))\nb'Python is beautiful' # Example 2\nprint(b'-'.join(lst))\nb'Python-is-beautiful' # Example 3\nprint(b'\\n'.join(lst))\nb'Python\\nis\\nbeautiful'<\/pre>\n<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>\n<h2>How to Fix &#8220;TypeError: sequence item 0: expected str instance, bytes found&#8221;?<\/h2>\n<p>A common mistake of many Python coders is to call the wrong <code>join()<\/code> method! Say, you&#8217;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>\n<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']\nprint(' '.join(lst))\n# TypeError: sequence item 0: expected str instance, bytes found<\/pre>\n<p>The error message doesn&#8217;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>\n<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>\n<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']\nprint(b' '.join(lst))\nb'Python is beautiful'<\/pre>\n<p>This way, you can easily concatenate multiple Byte objects by using the <code>Byte.join()<\/code> method. <\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When teaching the Finxter Python students, I&#8217;m often shocked how even intermediate-level coders do not understand the very basic foundations of computer science. In CS, there&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-113845","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=113845"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113845\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}