The basics of Python learning: coding

Source: Internet
Author: User

Reference URL: https://www.xncoding.com/2015/10/24/python/unicode.html

In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.

When editing with Notepad, the UTF-8 characters read from the file are converted to Unicode characters into memory, and when the edits are complete, the conversion of Unicode to UTF-8 is saved to the file:

When you browse the Web, the server converts dynamically generated Unicode content to UTF-8 and then to the browser:

So you see a lot of pages of the source code will have similar information, that the page is exactly the UTF-8 encoding

 >>> Print (' str with Chinese ')
Chinese-included str


>>> ' ABC '. Encode ( b ' ABC '
>> > "Chinese". Encode ( ' utf-8 ')
>>> ' Chinese '. Encode ( ' ASCII ')
traceback (most recent call last):
File "<stdin>", line 1, in <module>
unicodeencodeerror: ' ASCII ' codec Can ' t encode Characters in position 0-1: Ordinal not in range (+)


B ' ABC '. Decode (' ASCII ')
' ABC '
B ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' utf-8 ')
Chinese

When manipulating strings, we often encounter mutual conversions between Str and bytes. To avoid garbled problems, you should always use UTF-8 encoding to convert str and bytes.

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 basics of Python learning: coding

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.