第三章 字符串方法
3.1find可以在一个较长的字符串中查找子字符串,它返回子串所在位置的最左端索引,如果没有找到则返回-1
>>>title-"Monty Python's Flying Cirus"
>>>title.find('Monty')
0
>>>title.find('Python')
6
>>>title.find('Zirquss')
-1
也可为该方法提供可选的起始点和结束点参数
>>>subject='$$$ Get rich now!!! $$$'
>>>subject.find('$$$')
0
>>>subject.find('$$$',1)
20
>>>subject.find('!!!')
16
>>>subject.find('!!!',0,16)
-1
3.2join 用来在队列中添加元素(split的逆方法)
3.3lower返回字符串的小写字母版
3.4replace方法返回某字符串的所有匹配项均被替换之后得到字符串
>>>'This is a test'.replace('is','eez')
'Theez eez a test'
3.5split将字符串分割成序列
3.6strip方法返回去除两侧(不包括内部)空格的字符串
也可以指定需要去除的字符,将它们列为参数即可
>>>'***spam*for!!!!***'.strip('*!')
'spam*for'
3.7 translate处理单个字符