{"id":680,"date":"2024-11-08T05:36:12","date_gmt":"2024-11-08T05:36:12","guid":{"rendered":"https:\/\/itsmycode.com\/?p=680"},"modified":"2024-10-21T05:38:51","modified_gmt":"2024-10-21T05:38:51","slug":"python-convert-bytes-to-string","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-convert-bytes-to-string\/","title":{"rendered":"Python Convert Bytes to String"},"content":{"rendered":"\n<p>In this tutorial, we will take a look at how to convert bytes to string in Python.&nbsp;<\/p>\n\n\n\n<p>We can convert bytes to string using the below methods&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Using<code> decode() <\/code>method<\/strong><\/li>\n\n\n\n<li><strong>Using <code>str()<\/code> method<\/strong><\/li>\n\n\n\n<li><strong>Using <code>codecs.decode()<\/code> method<\/strong><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-1-using-decode-method\">Method 1: Using <code>decode()<\/code> method<\/h2>\n\n\n\n<p>The bytes class has a <strong><code>decode()<\/code><\/strong> method. It takes the byte object and converts it to string. It uses the UTF-8 encoding by default if you don\u2019t specify anything. The <strong><code>decode()<\/code><\/strong> method is nothing but the opposite of the encode.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python converting bytes to string using decode()\n\ndata = b'ItsMyCode \\xf0\\x9f\\x8d\\x95!'\nprint(data)\nprint(\"Before conversion type is\", type(data))\n\n# coversion happens from bytes to string\noutput = data.decode()\nprint(output)\nprint(\"Coverted type is \", type(output))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before conversion type is &lt;class 'bytes'&gt;\nItsMyCode \ud83c\udf55!\nCoverted type is  &lt;class 'str'&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-2-using-str-function\">Method 2: Using <code>str()<\/code> function<\/h2>\n\n\n\n<p>Another easiest way to convert from Bytes to string is using the <strong><code>str()<\/code><\/strong> method. You need to pass the correct encoding to this method else it will lead to incorrect conversion.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python converting bytes to string using str()\n\ndata = b'ItsMyCode \\xf0\\x9f\\x8d\\x95!'\nprint(data)\nprint(\"Before conversion type is\", type(data))\n\n# coversion happens from bytes to string\noutput = str(data,'UTF-8')\nprint(output)\nprint(\"Coverted type is \", type(output))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before conversion type is &lt;class 'bytes'&gt;\nItsMyCode \ud83c\udf55!\nCoverted type is  &lt;class 'str'&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-3-using-codecs-decode-method\">Method 3: Using <code>codecs.decode()<\/code> method<\/h2>\n\n\n\n<p><strong><code>codecs <\/code><\/strong>module comes as a standard built-in module in Python, and it has a <code><strong>decode()<\/strong><\/code> method which takes the input bytes and returns the string as output data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python converting bytes to string using decode()\nimport codecs\n\ndata = b'ItsMyCode \\xf0\\x9f\\x8d\\x95!'\nprint(data)\nprint(\"Before conversion type is\", type(data))\n\n# coversion happens from bytes to string\noutput = codecs.decode(data)\nprint(output)\nprint(\"Coverted type is \", type(output))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before conversion type is &lt;class 'bytes'&gt;\nItsMyCode \ud83c\udf55!\nCoverted type is  &lt;class 'str'&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will take a look at how to convert bytes to string in Python.&nbsp; We can convert bytes to string using the below methods&nbsp; Method 1: Using decode() method The bytes class has a decode() method. It takes the byte object and converts it to string. It uses the UTF-8 encoding by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":681,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,12,1],"tags":[],"class_list":["post-680","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-how-to","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=680"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/680\/revisions"}],"predecessor-version":[{"id":682,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/680\/revisions\/682"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/681"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}