首页 > 代码库 > Python 2.x and 3.x string difference

Python 2.x and 3.x string difference

 

In Python 3 unicode strings are the ‘regular strings‘ (str) and byte strings are separate objects.

Low level I/O can be done only with data (byte strings), not text (sequence of characters). For Python 2.x str was also the ‘binary data‘ type. In Python 3 it is not any more and one of the special ‘data‘ objects should be used. Objects are pickled to such byte strings. If you want to enter them manually in code use the "b" prefix (b"XXX" instead of "XXX").

bytes和string并不是毫无关系的,bytes对象有一个decode()方法,向该方法传递一个字符编码参数,该方法会返回使用该种编码解码后的字符串。同样的,string有一个encode()方法,完成反向的工作。

Python 2.x and 3.x string difference