Python Coding issues

Source: Internet
Author: User

1. The default encoding of the string in the code is consistent with the encoding of the code file itself.

such as: str = ' Chinese '

If it is in a UTF8 file, the string is UTF8 encoded, and if it is in a gb2312 file, it is encoded as gb2312.

#_ *_encoding:gbk_*_

str = ' Hello '

Str.decode (' GBK ')

Str.encode (' Unicode ')


2. Python uses Unicode encoding internally. Unicode is used as an intermediate encoding in which other encoded strings are decoded (decode) into Unicode and then encoded from Unicode (encode) to another encoding.

If the string is defined like this: S=u ' Chinese '

The encoding of the string is specified as Unicode, which is the internal encoding of Python, regardless of the encoding of the code file itself.

In some Ides, the output of a string is always garbled, or even wrong, because the IDE's result output console itself cannot display the encoding of the string, rather than the problem of the program itself.


The encode of Python string and the solution to the problem of decode

Http://www.jb51.net/article/17560.htm


Python Coding issues

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.