A detailed introduction to the Chinese full guide to Python

Source: Internet
Author: User

In the process of powerful computer language application, if you want to better understand Python's full Chinese strategy, you can watch our article, our article is about its related content. The following describes the relevant content.

Use Chinese in Python

It has two default strings: str and unicode. When using it, be sure to distinguish between "Unicode string" and "unicode object. All subsequent "unicode strings" refer to "unicode objects" in python ".
In fact, in Python, there is no such thing as a "Unicode string", but only a "unicode" object. A traditional unicode string can be expressed as a str object. It is only a byte stream at this time, unless decoded as a unicode object, there is no practical significance.

We use "Haha" to test on multiple platforms. The codes corresponding to "Haha" are:

  1. 1. UNICODE (UTF8-16), C854;  
  2. 2. UTF-8, E59388;  
  3. 3. GBK, B9FE。  

Windows Console

The following is the running result on the windows console:

It can be seen that in the console, the Chinese character encoding is GBK rather than UTF-16. Returns the same unicode object.
Note: printing ss on the console does not mean it can be serialized directly, for example:

Directly output ss to the file will throw the same exception. When processing unicode Chinese strings, you must first call the encode function to convert them to other encoding outputs. This is the same for all environments.
Summary: in Python, the "str" object is a byte array, and whether the content in it is a legal string and what encoding the string uses gbk, UTF-8, unicode) is not important.

The content must be recorded and determined by the user. These restrictions also apply to unicode objects. Remember that the content in the "unicode" object is definitely not necessarily a valid unicode string. We will soon see this situation.
Summary: on the windows console, str objects that support gbk encoding and unicode objects that support unicode encoding are supported.

  • Analysis of Python global variables in Python
  • A detailed explanation of Chinese Python Problems
  • Tips for sharing Python Chinese Characters
  • Adaptation and processing of related files in Python
  • How to convert Python to Chinese

Windows IDLE runs on Shell)

In windows IDLE, the running effect is different from that in the windows console:

It can be seen that for strings that do not use "u" as the identifier, IDLE encodes the Chinese characters into GBK. However, for unicode strings using "u", IDLE uses GBK encoding. The difference is that every character is a unicode object !! Len (ss) = 4. This creates a magical problem. The current ss cannot be normally displayed in IDLE. And I cannot convert ss to normal code!

For example, the following method is used:

This may be because the IDLE localization is not good enough to support Chinese characters. We recommend that you do not use the u "Chinese" method in the idle shell, because it is not what you want.
It also indicates that the Shell of IDLE supports two formats of Chinese strings: the "str" Object encoded by GBK, and the UNICODE object encoded by unicode.

The above content is part of our introduction to Python's full Chinese strategy.


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: 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.