在编写 Python 代码时,可能需要在一个字符串中搜索和定位另一个特定字符串。
这就是 Python 内置字符串方法派上用场的地方。
在本文中,您将学习如何使用 Python 内置的 find()
字符串方法来帮助您搜索字符串中的子字符串。
find()
方法 - 语法概述
find()
字符串方法内置于 Python 的标准库中。
它接受一个子字符串作为输入,并查找它的下标 - 即子字符串在您调用该方法的字符串中的位置。
find()
方法的一般语法如下所示:
string_object.find("substring", start_index_number, end_index_number)
让我们分解一下:
string_object
是您正在使用的原始字符串,也是您将调用find()
方法的字符串。这可以是您要搜索的任何词。find()
方法接受三个参数——一个是必需的,两个是可选的。- “substring” 是第一个必需的参数。这是您要在
string_object
中查找的子字符串。确保包含引号。 start_index_number
是第二个参数,它是可选的。它指定起始索引和搜索开始的位置。默认值为 0。end_index_number
是第三个参数,也是可选的。它指定结束索引和搜索停止的位置。默认值是字符串的长度。start_index_number
和end_index_number
共同指定了搜索范围,它们将搜索范围缩小到特定部分。
find()
方法的返回值是一个整数值。
如果字符串中存在子字符串,则 find()
返回给定字符串中指定子字符串第一次出现的索引或字符位置。
如果您要搜索的子字符串不在字符串中,则 find()
将返回 -1。它不会抛出异常。
如何使用没有开始和结束参数的 find()
示例
以下示例说明如何使用 find()
方法使用唯一必需的参数——您要搜索的子字符串。
您可以使用一个单词并搜索以查找特定字母的索引号:
fave_phrase = "Hello world!"
# find the index of the letter 'w'
search_fave_phrase = fave_phrase.find("w")
print(search_fave_phrase)
#output
# 6
我创建了一个名为 fave_phrase
的变量并存储了字符串 Hello world!。
我在包含字符串的变量上调用了 find() 方法,并在 Hello world! 中搜索字母“w”。
我将操作结果存储在名为 search_fave_phrase
的变量中,然后将其内容打印到控制台。
返回值是 w 的索引下标,在本例中是整数 6。
请记住,编程和计算机科学中的索引通常总是从 0 而不是 1 开始。
如何使用 find()
的 start 和 end 参数示例
在 find() 方法中使用 start 和 end 参数可以限制搜索范围。
例如,如果你想找到字母 ‘w’ 的索引并从位置 3 开始搜索而不是更早,你可以执行以下操作:
fave_phrase = "Hello world!"
# find the index of the letter 'w' starting from position 3
search_fave_phrase = fave_phrase.find("w",3)
print(search_fave_phrase)
#output
# 6
由于搜索从位置 3 开始,因此返回值将是从该位置开始及之后包含“w”的字符串的第一个实例。
您还可以使用 end 参数进一步缩小搜索范围并更加具体地搜索:
fave_phrase = "Hello world!"
# find the index of the letter 'w' between the positions 3 and 8
search_fave_phrase = fave_phrase.find("w",3,8)
print(search_fave_phrase)
#output
# 6
未找到子字符串的示例
如前所述,如果您使用 find()
指定的子字符串不存在于字符串中,则输出将为 -1 而不是异常。
fave_phrase = "Hello world!"
# search for the index of the letter 'a' in "Hello world"
search_fave_phrase = fave_phrase.find("a")
print(search_fave_phrase)
# -1
find()
方法区分大小写吗?
如果您搜索不同大小写的字母会怎样?
fave_phrase = "Hello world!"
#search for the index of the letter 'W' capitalized
search_fave_phrase = fave_phrase.find("W")
print(search_fave_phrase)
#output
# -1
在前面的示例中,在“Hello world!”中搜索字母 w 的索引。 find() 方法返回了它的位置。
在这种情况下,搜索大写字母 W 会返回 -1——这意味着该字母不存在于字符串中。
因此,在使用 find() 方法搜索子字符串时,是区分大小写的。
find()
方法与 in
关键字有什么区别?
首先使用 in 关键字检查字符串中是否存在子字符串。
in 关键字的一般语法如下:
substring in string
in 关键字返回一个布尔值——一个为 True 或 False 的值。
>>> "w" in "Hello world!"
True
当字符串中存在子字符串时,in 运算符返回 True。
如果子字符串不存在,则返回 False:
>>> "a" in "Hello world!"
False
在使用 find() 方法之前,使用 in 关键字是很有帮助的第一步。
首先检查字符串是否包含子字符串,然后可以使用 find() 找到子字符串的位置。这样,您就可以确定子字符串存在。
因此,使用 find() 来查找字符串中子字符串的索引位置,而不是查看子字符串是否存在于字符串中。
find()
方法与 index()
方法有什么区别?
与 find() 方法类似, index() 方法是一种字符串方法,用于查找字符串中子字符串的索引。
因此,这两种方法的工作方式相同。
这两种方法的区别在于,当字符串中不存在子字符串时,index() 方法会引发异常,这与返回 -1 值的 find() 方法相反。
fave_phrase = "Hello world!"
# search for the index of the letter 'a' in 'Hello world!'
search_fave_phrase = fave_phrase.index("a")
print(search_fave_phrase)
#output
# Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_article/demopython.py", line 4, in <module>
# search_fave_phrase = fave_phrase.index("a")
# ValueError: substring not found
上面的示例显示当子字符串不存在时 index() 会抛出 ValueError。
当您不想捕获和处理程序中的任何异常时,您可能希望使用 find() 而不是 index()。
总结
您现在知道如何使用 find() 方法在字符串中搜索子字符串。
希望本教程对您有所帮助。
感谢您的阅读,祝您编码愉快!