原文:Python find() – How to Search for a Substring in a String,作者:Dionysia Lemonaki

当你使用 Python 程序时,你可能需要在另一个字符串中搜索和定位特定字符串。

这就是 Python 的内置字符串方法派上用场的地方。

在本文中,你将学习如何使用 Python 内置的 find() 字符串方法来帮助你在字符串中搜索子字符串。

以下是我们将介绍的内容:

  • find() 方法的语法
  • 如何使用没有开始和结束参数的 find() 示例
  • 如何使用带有开始和结束参数的 find() 示例
  • 未找到子字符串示例
  • find() 方法是否区分大小写
  • find() vs in 关键字
  • find() vs index()

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_numberend_index_number 都指定了搜索将发生的范围,并将搜索范围缩小到特定部分。

如果子字符串存在于字符串中,则 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() 示例

使用带有开始和结束参数的 find() 方法可以限制搜索。

例如,如果你想查找字母 “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() vs in 关键字

首先使用 in 关键字检查子字符串是否存在于字符串中。

in 关键字的一般语法如下:

substring in string

in 关键字返回一个布尔值——一个 TrueFalse 值。

>>> "w" in "Hello world!"
True

当字符串中存在子字符串时,in 运算符返回 True

如果子字符串不存在,则返回 False

>>> "a" in "Hello world!"
False

在使用 find() 方法之前,使用 in 关键字是有用的第一步。

你首先检查字符串是否包含子字符串,然后你可以使用 find() 查找子字符串的位置。这样,你可以确定子字符串存在。

因此,使用 find() 来查找字符串中子字符串的索引位置,而不是查看字符串中是否存在子字符串。

find() vs index()

find() 方法类似,index() 方法是一种字符串方法,用于查找字符串中子字符串的索引。

因此,这两种方法的工作方式相同。

这两种方法的区别在于 index() 方法会在字符串中不存在子字符串时引发异常,而 find() 方法则返回 -1 值。

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() 方法在字符串中搜索子字符串。

我希望本教程对你有所帮助。

要了解有关 Python 编程语言的更多信息,请查看 freeCodeCamp 的 Python 认证。你将从基础开始,学习对初学者友好的交互式课程。你还将构建五个项目以加强对所学概念的理解。

感谢你阅读本文,祝你编码愉快!