Python string and encoding

Source: Internet
Author: User

At the earliest, only 127 characters were encoded into the computer, that is, uppercase and lowercase letters, numbers, and some symbols, the code is ASCII encoded.

But to deal with the Chinese is clearly a byte is not enough, at least two bytes, and can not conflict with ASCII encoding, so China has developed a GB2312 code, used to put Chinese into.

There are many languages in the world, countries have national standards, there will inevitably be conflicts, the result is mixed language in the text will appear garbled. As a result, Unicode emerges. Unicode unifies all the languages into a set of encodings so that no more garbled questions are made. The Unicode standard is evolving, but the most common use is to represent one character in two bytes.

If unified into Unicode encoding, garbled problem disappears, but if the text is basically all in English, Unicode encoding than ASCII encoding to more than the storage space, in storage and transmission is very not cost-effective. In this way, the UTF-8 encoding of converting Unicode encoding to "Edge length encoding" is presented. The UTF-8 encoding encodes a Unicode character into 1-6 bytes according to a different number size, the commonly used English letter is encoded in 1 bytes, the kanji is usually 3 bytes, only the very uncommon characters will be encoded into 4-6 bytes, if the text to be transferred contains a large number of English characters, You can save space by coding with UTF-8.

ASCII encoding can actually be seen as part of the UTF-8 encoding.

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.

In the latest version of Python 3, strings are encoded in Unicode.

For the encoding of a single character, Python provides an integer representation of the Ord () function to get the character, and the Chr () function converts the encoding to the corresponding character.

Because Python's string type is str, it is represented in memory in Unicode and a character corresponds to several bytes. If you want to transfer on the network, or save to disk, you need to turn str into bytes in bytes.

Python uses single or double quotation marks with a B prefix for data of type bytes: x = B ' ABC '. Be aware of the distinction between ' abc ' and ' B ' abc ', which is STR, although the content is the same as the former, but each character of bytes occupies only one byte.

The STR represented in Unicode can be encoded as a specified bytes by using the Encode () method, for example:

' ABC '. Encode (' ASCII ')

' Chinese '. Encode (' Utf-8 ')

The English-language STR can be ASCII encoded as bytes, the content is the same, the Chinese-containing STR can be encoded with UTF-8 bytes.

Conversely, if we read the byte stream from the network or disk, then the data read is bytes. To turn bytes into STR, you need to use the Decode () method:

B ' ABC '. Decode (' ASCII ')

To calculate how many characters str contains, you can use the Len () function. The Len () function calculates the number of characters in Str, and computes the number of bytes if replaced by the Bytes,len () function.

Formatting

In Python, the format used is the same as the C language and is implemented in%.

Inside the string,%s is replaced with a string,%d is replaced with an integer,%f floating-point number,%x hexadecimal integer. where formatted integers and floating-point numbers can also specify whether to complement 0 and the number of digits of integers and decimals. Like what:

Print ('%2d-%02d '% (3, 1))

Print ('%.2f '% 3.1415926).

If you're not sure what to use,%s will always work, and it will convert any data type to a string.

Format: Another way of formatting a string is to use the format () method of the string, which in turn replaces the placeholder {0}, {1} ... in the string with the passed-in parameter, but this is a much more troublesome way to write than%:

' Hello, {0}, the result increased {1:.1f}% '. Format (' Xiao Ming ', 12,123), printed as: Hello, Xiao Ming, the results increased by 17.1%.

Python string and 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.