Unicode strings in Python

Source: Internet
Author: User
Tags uppercase letter

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 a single word energy-saving representation of the largest integer is 255 (binary 11111111 = decimal 255), 0-255 is used to denote uppercase and lowercase letters, numbers, and some symbols, which are called ASCII encoding , such as the uppercase letter A encoding is a, small letter Z is encoded 122 .

if you want to represent Chinese, it is clear that a byte is not enough, requires at least two bytes, and does not conflict with ASCII encoding, so China has developed GB2312 coding, used to put Chinese in.

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 ordinary strings ' ABC ' inside of Python are all ASCII encoding the.

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

Print U ' Chinese '
Chinese

Note: without u, Chinese will not display properly.

Unicode string except for one more u , the escape character and multiline notation are still valid, except for 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.

Task

The following Tang poetry is represented by a multiline Unicode string and printed:

Quiet Night thinking

Before the bed was bright moonlight,

Suspicion is ground frost.

Jutou look at the moon,

Think of your hometown.

Reference code:

Print U ' Silent Night thinking
Before the bed was bright moonlight,
Suspicion is ground frost.
Jutou look at the moon,
Think of your hometown. ‘‘‘

or:

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

print ' Quiet Night thinking '

Before the bed was bright moonlight,

Suspicion is ground frost.

Jutou look at the moon,

Think of your hometown. ‘‘‘

Explanation: The code-editing first line of comments #-*-coding:utf-8-*-, indicating that the following Python code uses UTF-8 encoding, but many people write the output of the title to print U ' "...", so that the interpreter uses Unicode encoded output. Causes a decoding error. We should remember how the encoding should be decoded in any way, otherwise there is a good chance of coding errors.

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.