Python third-party Library series 15--code library

Source: Internet
Author: User

First, the previous picture:

We know: 1 bytes = 8 bits


Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and ordinary strings were ‘ABC‘ ASCII-encoded inside python. Python provides the Ord () and Chr () functions to convert letters and corresponding numbers to each other:

>>> ord(‘A‘)65>>> chr(65)‘A‘

Python later added support for Unicode, expressed in Unicode as a string u‘...‘ , for example:

>>> print u‘中文‘中文>>> u‘中‘u‘\u4e2d‘

Writeu‘中‘Andu‘\u4e2d‘is the same,\uThe hexadecimal Unicode code is followed. Sou‘A‘Andu‘\u0041‘is the same.

How do two strings convert to each other? Although the string ‘xxx‘ is ASCII encoded, it can also be seen as UTF-8 encoding, and u‘xxx‘ only Unicode encoding.

u ' xxx ' converted to UTF-8 encoded ' xxx ' with encode (' Utf-8 ') method:

>>> u ' abc '. Encode (' utf-8 ') ' abc ' >>> u ' Chinese '. Encode (' utf-8 ') ' \xe4\ xb8\xad\xe6\x96\x87 '  

English characters are converted to represent UTF-8 values that are equal to Unicode values (but occupy different storage spaces), while Chinese characters convert 1 Unicode characters into 3 UTF-8 characters, and you see \xe4 is one of the bytes, because its value is 228 , no corresponding letter can be displayed, so the numeric value of the byte is displayed in hexadecimal. len () function can return the length of a string:

>>> len (U ' abc ') 3>>> len (' abc ') 3>>> len (U ' Chinese ') 2>> > Len (' \xe4\xb8\xad\xe6\x96\x87 ') 6  

in turn, the UTF-8 code represents the string ' xxx ' converted to a Unicode string u ' xxx ' with decode (' Utf-8 ') method:

>>> ‘abc‘.decode(‘utf-8‘)u‘abc‘>>> ‘\xe4\xb8\xad\xe6\x96\x87‘.decode(‘utf-8‘)u‘\u4e2d\u6587‘>>> print ‘\xe4\xb8\xad\xe6\x96\x87‘.decode(‘utf-8‘)中文

Because the Python source code is also a text file, so when your source code contains Chinese, it is important to specify that you save it as UTF-8 encoding when you save it. When the Python interpreter reads the source code, in order for it to be read by UTF-8 encoding, we usually write these two lines at the beginning of the file:

#!/usr/bin/env python# -*- coding: utf-8 -*-

The first line of comments is to tell the Linux/os x system that this is a python executable and the Windows system ignores this comment;

The second line of comments is to tell the Python interpreter to read the source code according to the UTF-8 encoding, otherwise the Chinese output you write in the source code may be garbled.


Python third-party Library series 15--code library

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.