Chinese character string Truncation in Python,

Source: Internet
Author: User

Chinese character string Truncation in Python,

Let's look at an example:

#-*-Coding: utf8-*-s = U' Chinese cut's. decode ('utf8') [0: 3]. encode ('utf8') # result U' Chinese Truncation

Additional reading:

How many bytes of Chinese characters in the UTF-8?
Of 2 bytes:
3 bytes: basically equivalent to GBK, containing more than 21000 Chinese Characters
Four bytes: Chinese characters in the Chinese-Japanese character set, with more than 50 thousand characters
A UTF-8 number occupies 1 byte
1 byte for a UTF-8 letter

In the search for UTF-8 encoding data found that many posts said UTF-8 encoding, a Chinese character occupies 3 bytes, some also made a proof, probably like this, create a text file without BOM UTF-8 encoding, which saves several Chinese characters and then views the file size. I think this proof is not convincing, because the UTF-8 is extended, 1-6 bytes, a small number of Chinese Character detection is not that all Chinese characters are.

Later, I checked the character ing table-"Chinese" and found the correct answer. A few Chinese characters occupy three bytes, most of which occupy four bytes.

The above is all the content of this article. I hope you will like it.

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.