Python language learning 8--strings and encodings

Source: Internet
Author: User
Tags coding standards

Unicode encoding

A computer can only handle numbers, and if you want to work with text, you must convert the text to a number to handle

There are many coding standards, but different coding standards are sometimes confusing, so Unicode comes into being

Unicode unifies all languages into a set of encodings so that no more garbled problems

The difference between ASCII encoding and Unicode encoding:

ASCII encoding is 1 bytes, while Unicode encoding is usually 2 bytes.

Letters A with ASCII encoding are decimal 65 , binary 01000001 ;

Characters 0 with ASCII encoding are decimal 48 , binary 00110000 , and note that ‘0‘ the characters and integers 0 are different;

Chinese characters are beyond the ASCII encoding range, Unicode encoding is decimal 20013 , binary 01001110 00101101 .

You can guess that if you encode ASCII code in A Unicode, you just need to make 0 in front, so A the Unicode encoding is 00000000 01000001 .

The new problem arises again: If Unicode encoding is unified, the garbled problem disappears. However, if you write text that is basically all in English, using Unicode encoding requires more storage space than ASCII encoding, which is not cost-effective in storage and transmission.

UTF-8 encoding

Therefore, in the spirit of saving, there has been the conversion of Unicode encoding to "Variable length encoding" UTF-8 encoding. 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, and only the very uncommon characters are encoded into 4-6 bytes. If the text you want to transfer contains a large number of English characters, you can save space with UTF-8 encoding:

ASCII encoding can actually be seen as part of the UTF-8 encoding, so a large number of legacy software that only supports ASCII encoding can continue to work under 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.

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 <meta charset="UTF-8" /> information, that the page is exactly the UTF-8 encoding.

a python string

In the latest version of Python 3, strings are encoded in Unicode, meaning that Python's strings support multiple languages

The string type of Python is str,

Len () function

Formatting

%d integers

%f floating Point

%s string

%x Hex

Python language learning 8--strings and encodings

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.