1、find
find方法可以在一个较长的字符串中查找一个子字符串,它返回子串所在位置的最左端索引,如果没有找到则返回-1
>>> 'with a moo-moo here, and a moo-moo there'.find('moo')
7
>>> 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
2、join
join方法是非常重要的字符串方法,它是split方法的逆方法,用来在队列中添加元素:
>>> seq=[1,2,3,4,5]
>>> sep='+'
>>> sep.join(seq)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
sep.join(seq)
TypeError: sequence item 0: expected string, int found
>>> seq=['1','2','3','4','5']
>>> sep.join(seq)
'1+2+3+4+5'
>>> dirs='','usr','bin','env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:'+''.join(dirs)
C:usrbinenv
3、lower
lower方法返回字符串的小写字母版
>>> 'HDWUD HDJHS LKJDS'.lower()
'hdwud hdjhs lkjds'
4、replace
replace方法返回某字符串的所有匹配项均被替换后得到字符串
>>> 'This is a test'.replace('is','eez')
'Theez eez a test'
5、split
它是join的逆方法,用来将字符串分割成序列
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> 'C:usrbinenv'.split('/')
['C:usrx08inenv']
>>> 'Using the default'.split()
['Using', 'the', 'default']
注意:如果不提供任何分隔符,程序会把所有的空格作为分隔符
6、strip
strip方法返回去除两侧(不包含内部)空格的字符串:
>>> ' internal whitespace is kept '.strip()
'internal whitespace is kept'
也可指定需要去除的字符,将它们列为参数即可:
>>> '*** SPAM * for * everyone!!! ***'.strip(' *!')
'SPAM * for * everyone'
注意:只会去除两侧的字符
7、translate
translate方法和replace方法一样,可以替换字符串中的某些部分,但是和前者不同的是,translate方法只处理单个字符










