The Taichetaiwu of python-code

Source: Internet
Author: User

1. Understand the origins of the various encodings and their storage within the computer: http://www.cnblogs.com/JohnABC/p/3507219.html

2. Learn about the differences between binary and text files: http://www.cnblogs.com/JohnABC/p/3478134.html

3. Understanding the Unicode and str encodings of Python: http://www.cnblogs.com/JohnABC/p/4015504.html

4. Correct use of unicode:http://www.cnblogs.com/johnabc/p/4257574.html in Python

Summarize the following points according to the above articles:

1.both STR and Unicode are subclasses of basestring, and in strict sense, str is actually a byte string, which is a sequence of Unicode encoded bytes

2. The UTF-8 encoded str ' Han ' uses the Len () function, the result is 3, because in fact, UTF-8 encoded ' han ' = = ' \xe6\xb1\x89 '

3. Unicode is the real string, which is obtained after decoding the byte string str with the correct character encoding, and Len (U ' han ') = = 1

4. repr () and ' are doing exactly the same thing, they return an "official" string representation of an object, which means that in most cases the object can be re-obtained by the evaluation operation (using the built-in function eval ()), but Str () is different, str () A good-readability string representation of an object, whose return result is usually not used for eval () evaluation, but is well suited for print statement output, and it needs to be reminded again that not all strings returned by REPR () are able to use the eval () built-in function to get the original object

u = U ' han 'print#  u ' \u6c49 's = u.encode ('UTF-8 ' )print#  ' \xe6\xb1\x89 'u2 = S.decode ('UTF-8'  )print#  u ' \u6c49 '

5. Principles of Use

    • All text strings should be of the Unicode type, not the str type. If you are working with text, and the variable type is str, this is the bug!
    • To decode the byte string into strings, you need to use the correct decoding, that is, var.decode(encoding) (for example, var.decode(‘utf-8‘) ). Encodes a text string into bytes, using Var.encode (encoding).
    • Never use a Unicode string str() , or use a byte string without specifying an encoding unicode() .
    • When the app reads data externally, it should be treated as a byte string, which is the type of STR, followed by .decode() a call to interpret it as text. Similarly, when you send text to the outside, it is always called on the text .encode() .
    • If you use string literals in your code to represent text, you should always have a ' u ' prefix. In practice, however, you should never define the original string literals in your code. Anyway, I hate this one myself, and maybe others are just like me.

The Taichetaiwu of python-code

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.