Python Basic Learning 7----encoding and decoding

Source: Internet
Author: User

I. Python2

ASCII-encoded by default in Python2

str= ' Hello World ' gbk_to_unicode=str.decode (' GBK ') #将gbk解码为unicodeprint (Gbk_to_unicode) unicode_to_utf-8=gbk_to_ Unicode.encode (' Utf-8 ') #将unicode编码为utf -8print (unicode_to_utf-8)

Two. Python3

Python3 text is encoded by default in Unicode

str= ' Hello World ' unicode_to_gbk=str.encode (' GBK ') print (UNICODE_TO_GBK) #此处输出为byte类型的gbk编码的内容, That is, the string is converted to the byte type Gbk_to_utf-8=unicode_to_gbk.decode (' Utf-8 ') at the same time as the encoding #此处在解码的同时将byte转换成了字符串print (GBK_TO_UTF-8)

  

Feel the content here is somewhat complex, do not dare to confirm the above content is correct, but also please pass by the predecessors to add guidance.

Python Basic Learning 7----encoding and decoding

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: info-contact@alibabacloud.com 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.