{"id":3657,"date":"2022-12-01T21:04:13","date_gmt":"2022-12-01T21:04:13","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3657"},"modified":"2022-12-01T21:04:15","modified_gmt":"2022-12-01T21:04:15","slug":"python-string-encode","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-encode\/","title":{"rendered":"Python String encode() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String encode<\/p>\n<p>Now, we are going to learn about the encoding method. As the name of the method suggests, it is going to return an encoded version of the string, as a bytes object. The default encoding is &#8216;utf \u2013 8&#8217;.<\/p>\n<p>Let\u2019s have a look at a simple program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3658 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-encode-METHOD.jpg\" alt=\"\" width=\"621\" height=\"80\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-encode-METHOD.jpg 621w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-encode-METHOD-300x39.jpg 300w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String encode() Method<\/h2>\n<p>As you can see in the above program, we have a string, and then we are calling the encode method, which returns the encoded version of the string, as a bytes object. Let\u2019s have a look at the output now &#8211;<\/p>\n<p>b&#8217;Hello from GyaniPandit&#8217;<\/p>\n<p>As you can see that we have the encoded string as a bytes object. So, as and when required, we can make use of the encode method in relation to the string in python. We can provide the encoding as an argument to the encoding method, but the default is &#8216;utf \u2013 8&#8217;. Another popular encoding that you might be familiar with is ascii, and there are some others as well.<\/p>\n<p>Also, we can specify another argument, as errors, which defaults to &#8216;strict&#8217;. It is the response when the encoding fails. There are different values for this argument, like &#8216;strict&#8217;, &#8216;ignore&#8217;, &#8216;replace&#8217; etc&#8230;<\/p>\n<ul>\n<li>The argument value &#8216;strict&#8217; specifies that if the encoding fails, it raises an UnicodeDecodeError.<\/li>\n<li>The argument value &#8216;ignore&#8217; ignores the unencodable Unicode from the result.<\/li>\n<li>The argument value &#8216;replace&#8217; replaces the unencodable Unicode with a question mark(?).<\/li>\n<\/ul>\n<p>So, as and when required, we can make use of the encoding method. Remember that this method returns the encoded version of the string, as a bytes object. You can also check the type of the return value from the encode method. We can also specify the encoding, and the default is &#8216;utf \u2013 8&#8217;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String encode Now, we are going to learn about the encoding method. As the name of the method suggests, it is going to return an encoded version of the string, as a bytes object. The default encoding is &#8216;utf \u2013 8&#8217;. Let\u2019s have a look at a simple program, which demonstrates the same thing. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3657","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3657"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3657\/revisions"}],"predecessor-version":[{"id":4180,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3657\/revisions\/4180"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}