freeCodeCamp/guide/chinese/python/string-methods/string-find-method/index.md

962 B

title localeTitle
String Find Method 字符串查找方法

字符串查找方法

在Python中find()字符串中的子字符串有两种选择: find()rfind()

每个都将返回找到子字符串的位置。两者之间的区别在于find()返回最低位置, rfind()返回最高位置。

可以提供可选的开始和结束参数,以限制在字符串的部分内搜索子字符串。

例:

>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!" 
 >>> string.find('you') 
 6 
 >>> string.rfind('you') 
 42 

如果未找到子字符串,则返回-1。

>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!" 
 >>> string.find('you', 43)  # find 'you' in string anywhere from position 43 to the end of the string 
 -1 

更多信息:

字符串方法文档