用dir(str)可以查询字符串的所有方法
1.find
语法:str.find(sub, [start,end) ) \\\\\根据指定的数据查找对应的索引
解释:在字符串中查找子串,如果找到,返回子串的第一个字符的index,否则返回 -1。
note1:可以指定搜索的范围,[start,end),find方法返回的不是布尔值,找到返回index,找不到返回-1。
note2:如果在这个字符串中存在多个很多个sub,则返回第一个sub的第一个字符的index。
>>> x = 'with a moo-moo here, and a moo-moo there'
>>> x.count('moo')
4
>>> x.find('moo')
7
>>> x.find('with')
0
>>> x.find('happy')
-1
>>> x.find('moo',11,17)
11
1.1 rfind
语法:str.rfind(sub,[start,end)) \\\\\根据指定的数据查找对应的索引
解释:返回找到的最后一个子串的第一个字符的索引,没有找到就返回-1,可以提供范围。
>>> x = 'with a moo-moo here, and a moo-moo there'
>>> x.rfind('moo')
31
>>> x.rfind('h