Python is and = = difference//encoding and decoding

Source: Internet
Author: User

A. Is and = = difference:

= = Compare #比较两边的值

is comparison #比较的是内存地址

ID () is a built-in function of Python, by ID () we can find the value of a variable table in memory address

(Numbers and strings are true only)

#数字小数据池: -5~256

1. If there are special characters in the string, they will not be added to the decimal pool and their memory address will be different.

Within a single * 20 of the string, as much as their memory, more than 20 will not be added to the small data pool; More than one memory address is different

Note: Pycharm is a pit, all the same strings in a py file, usually using the same memory address

In the py file, if you simply define a string, then the general will be added to the small data pool, we can think that: in the use of strings, Python will help us to cache the string, the next time we use a direct point to the string, you can save a lot of memory.

s = 2Print(ID (s))#1514368064s = 2Print(ID (s))#1514368064s ='Alex'Print(ID (s))#43156680s ="Alex"Print(ID (s))#43156680LST = [1, 2, 4]Print(ID (LST))#43164168Lst1 = [1, 2, 4]Print(ID (LST1))#41362568Tup = (1, 2) Tup1= (1, 2)Print(ID (TUP))#39978312Print(ID (tup1))#39978376


# We find that the data address of the number and the string is the same. The data addresses for lists and tuples are not the same

.

Two. Encoding and decoding

Encoude (encoded)-----The corresponding byte T after getting the plaintext code

Decode (encoded)-----decodes the encoded bytes into the corresponding plaintext

The ASCII code is used in 1.python2, so Chinese is not supported, and if you need to change the encoding in Python, you need to start writing in Chinese:

  # -*-encoding:utf-8-*-

Coding review;

1.ASCII: The earliest encoding, there are English capital letters, lowercase letters, numbers, some special characters, no Chinese.

8 bits (8 bits), 1 bytes.

2.GBK: Chinese GB Code, which contains ASCII code, 1

6 bit,2 a Byte

3.unicode: Universal code, which contains text codes for all countries of the world

32 bit,4 Byte, containing the ASCII

4.utf-8: A variable-length universal code, which is an implementation of Unicode, with a minimum character of 8 bits

English: 8bit 1byte

European text: 16bit 2byte

English: 24bit 3byte

In addition to the ASCII code, other information cannot be converted directly.

In the memory of Python3, Unicode encoding is used during the run of the program, because Unicode is a universal code and what content can be displayed, so when data transfer and storage are wasted space and resources due to Unicode comparison, Need to put Unicode into UTF-8 or CBK for storage, how to convert it, in Python can be encoded in the text message, the content after the encoding can be transmitted, the encoded data is bytes type of data, actually Ah, Or the original data is only encoded after the expression has changed.

Bytes form of expression:

1, English B ' Alex ', English expressions and strings are no different;

2, Chinese B ' \xe4\xb8\xad ', this is a Chinese character of the UTF-8 of the bytes expression form;

    #Code:s ="Alex"    Print(S.encode ("Utf-8"))#encode the string into UTF-8 with the result: B ' Alex '    Print(S.encode ("GBK"))#encode the string into GBK with the result: B ' Alex 's="in"    Print(S.encode ("UTF-8"))    Print(S.encode ("GBK")) S1= S.encode ("UTF-8")#Chinese encoded into UTF-8, the result is: B ' \xe4\xb8\xad 'S2 = S.encode ("GBK")#Chinese encoded into GBK, the result is: B ' \xd6\xd0 '    #Decoding:    Print(S1.decode ("UTF-8"))#The result is: Medium    Print(S2.decode ("GBK"))#The result is: MediumCopy Code

Note: What to encode is what to decode.

Remember: The result of the English encoding is the same as the original string, the result of the Chinese encoding according to the different encoding, encoding results are different, we can be sure that a Chinese UTF-8 is 3 bytes. The Chinese encoding for a GBK is 2 bytes.

Python is and = = difference//encoding and decoding

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.