Python3 Coding Problem Personal understanding

Source: Internet
Author: User

#coding =utf-8

A = "You" # This string is unicode and a = u "You" equivalent
b = B ' \\u4f60 ' #这个表示b是字节串 (Print (B.decode ("Unicode-escape"))) system will return "you" if you need to display the value of B because it decodes you based on Utf-8.
Print (A.encode ("Unicode-escape")) #这个表示a这个字符串编码成Unicode的字节串 The return value is B ' \\u4f60 '
Print (' \u4f60 ') #等价于 print (a) is equivalent to print (' you '), and the system will do the following when it is actually played
Print (' \u4f60 '. Encode ("Utf-8"). Decode ("Utf-8"))
#python默认使用utf-8 encoding, converts Unicode characters to utf-8 format, and then looks at the value of the codepage table to display the string "You"

Here's a long-time brain problem.

Print (b ' \u4f60 '. Decode ("Unicode-escape")) #返回 "You", where B ' \u4f60 ' is a Unicode byte string, so it needs to be decoded into Unicode characters
Print (' \u4f60 '. Encode ("Utf-8"). Decode ("Utf-8")) #返回 "You" Here "\u4f60" and "you" equivalent, encode Unicode into utf-8 format, then decode "You" according to the codepage table

The following because of the dead, so confuse themselves, figured out after finishing the content as follows.

The "You" Unicode byte string is B ' \\u4f60 '
The utf-8 byte string of "You" is B ' \xe4\xbd\xa0 '
Since Python3 is a Unicode format, the default encoding format is Utf-8
So print (' \u4f60 ') defaults to decoding it as a Unicode format to the Utf-8 string to display.
So the personal understanding is that print (' \u4f60 ') prints the return value to the user to view the operation of the computer is print (' \u4f60 '. Encode ("Utf-8"). Decode ("Utf-8"))
Print (' \xe4\xbd\xa0 ') is garbled because it is garbled in the Unicode codepage table (unless Python turns the string into utf-8 format, "You" can be displayed normally)

Python3 Coding Problem Personal understanding

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.