Home > Article > Backend Development > A brief discussion on character encoding and strings in python learning
This article brings you a brief discussion of character encoding and strings in Python learning. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is the character encoding?
For example, the Chinese character "中" can be represented by the following
decimal : 20013
Binary: 01001110 00101101(unicode)/11100100 10111000 10101101(utf-8)
Hexadecimal: u4e2d
ascii encoding
ASCII encoding is 1 bytes
Can only encode pure English
##Save space
unicode encoding
encoding is usually 2 bytes . (For example, the letter A encoded with ASCII is decimal 65, binary 01000001; A’s Unicode The encoding is 00000000 01000001.)
ascii, which is not cost-effective for storage and transmission (UTF-8Solution )
UTF-8
encoding puts aUnicode characters are encoded into 1-6 bytes according to different number sizes, and commonly used English letters are encoded into 1 bytes, Chinese characters are usually 3 bytes, only very rare characters will be encoded into 4 -6 bytes.
ASCII | Unicode | UTF-8 | |
01000001 | 00000000 01000001 | 01000001 | |
x | 01001110 00101101 | 11100100 10111000 10101101 |
%d | |
%f | |
%s | |
%x | |
Another way to format a string is to use the string's format()
method, which will use the passed in The parameters replace the placeholders{0}, {1}... in the string in sequence, but this way of writing is easier than % Much more troublesome: >>> 'Hello, {0}, the score has improved by {1:.1f}%'.format('Xiao Ming', 17.125) ' Hello, Xiao Ming, your score has improved by 17.1%'
The above is the detailed content of A brief discussion on character encoding and strings in python learning. For more information, please follow other related articles on the PHP Chinese website!