Unicode strings in Python

Source: Internet
Author: User

The string also has an encoding problem.

Because a computer can only handle numbers, if you are working with text, you must convert the text to a number before processing it. The oldest computer was designed with 8 bits (bit) as a byte (byte), so the largest integer that a Word energy saver represents is 255 (binary 11111111 = decimal 255), and 0-255 is used to denote uppercase and lowercase letters, numbers, and some symbols. This Code table is called ASCII encoding, such as the code for capital A is 65, and the lowercase z is encoded as 122.

If you want to express Chinese, obviously a byte is not enough, need at least two bytes, and also can't and ASCII encoding conflict, so, China has developed GB2312 code, used to put Chinese into.

Similarly, other languages such as Japanese and Korean also have this problem. In order to unify all text encoding, Unicode came into being. Unicode unifies all languages into a set of encodings, so there is no more garbled problem.

Unicode usually uses two bytes to represent a character, the original English encoding from a single byte into a double-byte, only need to fill the high byte all 0 can be.

Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and the normal string ' ABC ' was ASCII-encoded inside python.

Python later added support for Unicode, with a Unicode-represented string expressed in U ' ... ', for example:

Print U ' Chinese '

Note: Without u, Chinese will not display properly.

In addition to a single Unicode string  u  , the escape character and multiline notation are still valid, with no difference to the normal string:

Escape:

U ' chinese \ n \ japanese \ Korean '

MultiRow

U ' ' first line second line '

raw+ Multiple lines:

ur ' python's Unicode string supports "Chinese", "Japanese", "Korean" and many other languages "

If the Chinese string encounters unicodedecodeerror in a Python environment, this is because there is a problem with the format saved by the. py file. You can add comments on the first line

#-*-Coding:utf-8-*-

The purpose is to tell the Python interpreter to read the source code with UTF-8 encoding. Then save with notepad++ as ... and select UTF-8 format to save.

Unicode strings in Python

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.