python的str类

1. python 3,5中,class str是immutable sequences of Unicode code points,即以Unicode编码的。

2. 因为没有char类型,故对class str进行索引,将得到一个长度为1的str。

例如:a='ABC';

type(a[0])也是class str。

3. 初始化,

class str(object, encoding='utf-8', errors='strict')

返回object的string version。

如果object没有提供,则返回一个empty string。否则取决于encoding和errors是否提供。

如果encoding和errors都没有提供,则返回informal or nicely printable string representation of object。

即: 如果object是string, 则是string自身;object没有__str__方法,则返回repr(object)。

例如:str(b'xxx'),返回的是个class str,但内容是“b'xxx'”。

4. str.encode(encoding='utf-8', errors='strict')

将str对应的字符串进行编码, 返回bytes对象。例如utf-8编码。

参考资料:https://docs.python.org/3.6/library/stdtypes.html

猜你喜欢

转载自blog.csdn.net/ghalcyon/article/details/81977646