Python网络爬虫技术与实战
上QQ阅读APP看书,第一时间看更新

1.4.1 字符串运算符

字符串运算符的作用就是将两个字符串进行拼接,从而形成一个新的字符串。Python语言支持的字符串运算符如表1-2所示。

表1-2 Python字符串运算符表

字符串运算符的使用示例如下所示。

【例1-14】字符串连接“+”的用法示例


>>> mystr='hello'+' world'+' chuancy'
>>> print(mystr)

执行结果:


hello world chuancy

【例1-15】重复输出字符“*”的用法示例


>>> mystr=3*' hello'
>>> print(mystr)

执行结果:


hello hello hello

【例1-16】索引“[]”的用法示例


>>> str='hello'
>>> str[3]

执行结果:


'l'

【例1-17】字符串截取“[:]”的用法示例


>>> str='hello'
>>> str[1:3]

执行结果:


'el'                        # 索引从0开始,顾头不顾尾,不包含索引3的对象

【例1-18】成员运算符“in”的用法示例


>>> str='hello'
>>> 'e' in str

执行结果:


True