Python Encode () method (GO)

Source: Internet
Author: User

Transferred from:


The encode () method encodes a string in the specified encoding format, and the default encoding is ' Utf-8 '.

Corresponding decoding method: Bytes decode () method.


Encode () method syntax:



    • Encoding--optional parameter, the encoding to use, the default encoding is ' Utf-8 '.
    • Errors--optional parameter, set different error handling scheme. The default is ' strict ', which means a unicodeerror is caused by a coding error. Other possible values are ' ignore ', ' replace ', ' xmlcharrefreplace ', ' backslashreplace ' and any value registered through Codecs.register_error ().
return value

The method returns the encoded string, which is a bytes object.


#!/usr/bin/env python
s = " Beginner's tutorial "
S_utf8 = S.encode("utf-8")
S_GBK = S.encode("gbk")
Print (s)
Print ("", S_utf8)
Print ("", S_GBK)
Print ("", S_utf8. Decode (' Utf-8 '))
Print ("", S_GBK. Decode (' GBK '))


Beginner's Tutorial
Utf-8 code: B ' \xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b '
GBK code: B ' \XB2\XCB\XC4\XF1\XBD\XCC\XB3\XCC '
Utf-8 decoding: Beginner's tutorial
GBK decoding: Beginner's tutorial


    • STR uses the Decode method to decode it to a Unicode string type based on the encoding of STR.
    • STR uses encode to convert a Unicode string type to a specific encoding based on a specific encoding

Python Encode () method (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.