{"id":4477,"date":"2020-03-29T15:46:39","date_gmt":"2020-03-29T15:46:39","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4477"},"modified":"2023-02-16T19:57:08","modified_gmt":"2023-02-16T19:57:08","slug":"python-bytearray-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-bytearray-function","title":{"rendered":"Python bytearray() function"},"content":{"rendered":"\n<p>In this article, we will be having a look at one of the Python&#8217;s in-built function &#8212; <strong>Python bytearray() function<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Python bytearray() function<\/h2>\n\n\n\n<p><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/\">Python <\/a>has in-built bytearray() method that <strong>creates an array of bytes<\/strong> and <strong>returns a bytearray object<\/strong> of the created array of the defined size by a particular iterable or a value.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbytearray(source_input, encoding_scheme, error)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>source_input<\/code>: It is an optional parameter. It is basically used to initialize the array data elements. The source_input can be an <strong>iterable<\/strong>, <strong>value<\/strong>, etc.<\/li><li><code>encoding_scheme<\/code>(optional): It is used to define the encoding pattern for a string.<\/li><li><code>error<\/code>(optional): It defines the actions that need to be taken in case the encoding fails. <\/li><\/ul>\n\n\n\n<p>The <code>bytearray() method<\/code> takes an iterable such as list, string, tuple, etc or value as an argument and initializes an array with the size and returns a <strong>bytearray object<\/strong> of it.<\/p>\n\n\n\n<p><strong>Example:<\/strong> <strong>Python bytearray() function with no arguments<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp = bytearray()\nprint(inp)\n<\/pre><\/div>\n\n\n<p>When <strong>no argument<\/strong> is passed to the bytearray() function, the function returns an <strong>empty bytearray object<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbytearray(b&#039;&#039;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">1. Python bytearray() function with string as an argument<\/h3>\n\n\n\n<p>When a <strong>string value<\/strong> is passed as an argument to the bytearray() function, it converts the <strong>string into an array of bytes<\/strong>. <\/p>\n\n\n\n<p>The mandatory condition continues to stay such that whenever a string is passed in the parameter list, <strong>encoding <\/strong>has to be defined for the same in the parameter list, otherwise a <code>TypeError exception<\/code> is raised.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str = &quot;JournalDev&quot;\n\narr_8 = bytearray(inp_str, &#039;utf-8&#039;) \n\nprint(arr_8) \n\n<\/pre><\/div>\n\n\n<p>Here, we have passed the encoding_scheme as &#8216;<strong>utf-8<\/strong>&#8216; for the input string to be converted to an array of bytes.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbytearray(b&#039;JournalDev&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str = &quot;JournalDev&quot;\n\narr_16 = bytearray(inp_str, &#039;utf-16&#039;) \n\nprint(arr_16) \n<\/pre><\/div>\n\n\n<p>In the above example, the encoding scheme is defined as &#8216;<strong>utf-16<\/strong>&#8216;.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbytearray(b&#039;\\xff\\xfeJ\\x00o\\x00u\\x00r\\x00n\\x00a\\x00l\\x00D\\x00e\\x00v\\x00&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Python bytearray() function with iterable passed as a parameter<\/h3>\n\n\n\n<p>When an iterable such as <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">list<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/set\/python-set\" class=\"rank-math-link\">set<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">tuple<\/a>, etc is passed as an argument to the bytearray() function, it returns an array of bytes that contains the <strong>initial contents as the array elements<\/strong> in the bytearray object.<\/p>\n\n\n\n<p><strong>Mandatory condition:<\/strong> If an iterable is passed as an argument to the bytearray() function, it is necessary for all the elements of the iterable to be of type <code>integer<\/code> to avoid TypeError.<\/p>\n\n\n\n<p><strong>Example:<\/strong> <strong>Python bytearray() with list<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_lst = &#x5B;2, 4, 6, 8]\nres_byte = bytearray(inp_lst)\nprint(res_byte)\n<\/pre><\/div>\n\n\n<p>As clearly understood, <strong>the contents of the list i.e. [2,4,6,8]<\/strong> have been used to create a bytearray object.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbytearray(b&#039;\\x02\\x04\\x06\\x08&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Python bytearray() function with integer value as an argument<\/h3>\n\n\n\n<p>If the bytearray() function encounters an integer value as a parameter, it then creates a bytearray object with the <code>size = integer value<\/code> and <strong>initializes <\/strong>it with <strong>null values (&#8216;\\0&#8217;)<\/strong>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp = 14\nres_byte = bytearray(inp)\nprint(res_byte)\n<\/pre><\/div>\n\n\n<p>In the above example, a array object is created with the <strong>array size as &#8217;14&#8217;<\/strong> and initialized with null values.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbytearray(b&#039;\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python bytearray() function returns a <strong>bytearray object<\/strong> i.e. <strong>generates an array of bytes<\/strong> of the type of source input provided to the function.<\/li><li>The bytearray() function can have an <strong>iterable<\/strong>, <strong>values<\/strong>, etc as parameters to it.<\/li><li>Whenever an iterable is passed the function, it is essential for the elements of the iterable to be of <strong>integer type<\/strong> only.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the working of the Python bytearray() method with various types of parameters.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python bytearray() &#8211; JournalDev<\/li><li><a href=\"https:\/\/docs.python.org\/3.1\/library\/functions.html#bytearray\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python bytearray() function &#8211; Official Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be having a look at one of the Python&#8217;s in-built function &#8212; Python bytearray() function. Understanding Python bytearray() function Python has in-built bytearray() method that creates an array of bytes and returns a bytearray object of the created array of the defined size by a particular iterable or a value. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":4532,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-4477","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4477"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4477\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4532"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}