Python Learning character encoding

Source: Internet
Author: User

The string involves encoding: ASCII GBK gb2312 Unicode uft-8

For English characters ASCII (can be regarded as a subset of Utf-8), Chinese with gbk/gb2312;

Unicode: The world's unified computer system coding (in memory), but the storage is a waste of space, so save to the hard disk or output to view with uft-8. That is, when you open the reading with Notepad or browse the Web page is unicode-"Tuf-8, Because Notepad and the browser server system are Unicode types

Uft-8:ptthon when saving the source code, it is important to specify Save as UTF-8 encoding. At the beginning of the program, it is stated that UTF-8 encoding does not imply that your .py file is UTF-8 encoded, which is set by the text editor's settings-encoding type to use UTF-8 Without BOM encoding. If the .py file itself uses UTF-8 encoding and also declares # -*- coding: utf-8 -*- , open a command prompt to verify that Chinese is supported

In Python version 3, strings are Unicode-encoded and support Chinese

Python provides a ord(‘字符’) coded integer representation of the function to get the character, and the chr() function converts the encoding to the corresponding character

The string type of Python is a str Unicode representation in memory, a character that corresponds to several bytes, and a Unicode representation of the str pass. encode(‘ASCII’) Method can be encoded as the specifiedbytes

bytesType of data is represented by a b prefixed single or double quotation mark, used for string transmission over the network, or saved to disk

Len (' abc ') and Len (b ' ABC ') represent the number of characters and bytes respectively.

Formatted output: internal and external 22%

>>> ‘Hi, %s, you have $%d.‘ % (‘Michael‘, 1000000) ‘Hi, Michael, you have $1000000.‘

Python Learning character encoding

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.