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.