Python 字符串 find() 方法(附带示例)
什么是 Python 字符串 find()?
Python 字符串 find() 是 Python 库中一个查找给定字符串中子字符串首次出现索引的函数。如果未在给定字符串中找到指定的子字符串,字符串 find() 函数将返回 -1,而不是抛出异常。
Python 字符串 find() 的语法
Python find() 函数的基本语法如下:
string.find(substring,start,end)
find() 方法的参数
Python 字符串 find() 函数有三个参数:
- substring:您要在给定字符串中搜索的子字符串。
- start:(可选)搜索子字符串开始的位置。默认为 0。
- end:(可选)搜索子字符串结束的位置。默认值为字符串长度。
find() 方法的默认值示例
传递给 Python find() 方法的参数是 substring(即您要搜索的字符串)、start 和 end。start 值默认为 0,end 值是字符串的长度。
在此示例中,我们将使用 Python 中的 find() 方法,并使用默认值。
find() 方法将搜索子字符串并给出子字符串首次出现的位置。现在,如果子字符串在给定字符串中出现多次,它仍然会返回第一个的索引或位置。
示例
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Tutorials is at:", mystring.find("Tutorials"))
输出
The position of Tutorials is at: 12
使用 start 参数的 find() 示例
您可以搜索给定字符串中的子字符串,并指定开始位置,搜索将从该位置开始。start 参数可用于此目的。
该示例将 start 位置指定为 15,Python 中的 find() 方法将从位置 15 开始搜索。这里,end 位置将是字符串的长度,并将从位置 15 开始搜索直到字符串末尾。
示例
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Tutorials is at:", mystring.find("Tutorials", 20))
输出
The position of Tutorials is at 48
使用 start 和 end 参数的 find() 示例
使用 start 和 end 参数,我们将尝试限制搜索范围,而不是搜索整个字符串。
示例
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Tutorials is at:", mystring.find("Tutorials", 5, 30))
输出
The position of Tutorials is at 12
find() 方法示例:查找给定子字符串在字符串中的位置
我们知道 find() 帮助我们找到子字符串首次出现的索引。如果字符串中不存在该子字符串,它会返回 -1。下面的示例显示了字符串存在时的索引,以及在找不到要搜索的子字符串时返回 -1。
示例
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Best site is at:", mystring.find("Best site", 5, 40)) print("The position of Guru99 is at:", mystring.find("Guru99", 20))
输出
The position of Best site is at: 27 The position of Guru99 is at: -1
Python 字符串 rfind()
Python 函数 rfind() 与 find() 函数相似,唯一的区别是 rfind() 给出给定子字符串的最高索引,而 find() 给出最低索引,即第一个索引。如果找不到子字符串,rfind() 和 find() 都会返回 -1。
在下面的示例中,我们有一个字符串“Meet Guru99 Tutorials Site. Best site for Python Tutorials!”,我们将尝试使用 find() 和 rfind() 查找子字符串 Tutorials 的位置。Tutorials 在字符串中出现了两次。
这里有一个同时使用 find() 和 rfind() 的示例。
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Tutorials using find() : ", mystring.find("Tutorials")) print("The position of Tutorials using rfind() : ", mystring.rfind("Tutorials"))
输出
The position of Tutorials using find() : 12 The position of Tutorials using rfind() : 48
输出显示 find() 给出了它遇到的第一个 Tutorials 子字符串的索引,而 rfind() 给出了 Tutorials 子字符串的最后一个索引。
Python 字符串 index()
Python 字符串 index() 函数与 find() 类似,可以给出给定子字符串的位置。两者之间的唯一区别是,如果子字符串不存在于字符串中,index() 会抛出异常,而 find() 会返回 -1。
这是一个演示 index() 和 find() 行为的工作示例。
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Tutorials using find() : ", mystring.find("Tutorials")) print("The position of Tutorials using index() : ", mystring.index("Tutorials"))
输出
The position of Tutorials using find() : 12 The position of Tutorials using index() : 12
我们为 find() 和 index() 获得了相同的位置。让我们看一个子字符串不存在于字符串中的示例。
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!" print("The position of Tutorials using find() : ", mystring.find("test")) print("The position of Tutorials using index() : ", mystring.index("test"))
输出
The position of Tutorials using find() : -1 Traceback (most recent call last): File "task1.py", line 3, in <module> print("The position of Tutorials using index() : ", mystring.index("test")) ValueError: substring not found
在上面的示例中,我们试图找到子字符串“test”的位置。子字符串不存在于给定的字符串中,因此使用 find(),我们得到位置 -1,但对于 index(),它会像上面所示那样引发错误。
查找子字符串的总出现次数
要查找子字符串在给定字符串中出现的总次数,我们将使用 Python 中的 find() 函数。我们将使用 for 循环,从 0 到字符串末尾来遍历字符串。我们将为 find() 使用 startIndex 参数。
变量 startIndex 和 count 将初始化为 0。在 for 循环中,我们将使用 find() 和 startIndex 作为 0 来检查子字符串是否在给定的字符串中。
find() 返回的值(如果不是 -1)将更新 startIndex 为找到字符串的索引,并增加 count 值。
这是一个工作示例
my_string = "test string test, test string testing, test string test string" startIndex = 0 count = 0 for i in range(len(my_string)): k = my_string.find('test', startIndex) if(k != -1): startIndex = k+1 count += 1 k = 0 print("The total count of substring test is: ", count )
输出
The total count of substring test is: 6
摘要
- Python 字符串 find() 方法有助于查找子字符串在给定字符串中首次出现的索引。如果子字符串不存在,它将返回 -1。
- 传递给 Python find 字符串方法的参数是 substring(即您要搜索的字符串)、start 和 end。start 值默认为 0,end 值是字符串的长度。
- 您可以搜索给定字符串中的子字符串,并指定开始位置,搜索将从该位置开始。start 参数可用于此目的。
- 使用 start 和 end 参数,我们将尝试限制搜索范围,而不是搜索整个字符串。
- Python 函数 rfind() 与 find() 函数相似,唯一的区别是 rfind() 给出给定子字符串的最高索引,而 find() 给出最低索引,即第一个索引。如果找不到子字符串,rfind() 和 find() 都会返回 -1。
- Python 字符串 index() 是另一个函数,它与 find() 一样给出给定子字符串的位置。两者之间的唯一区别是,如果子字符串不存在于字符串中,index() 会抛出异常,而 find() 会返回 -1。
- 我们可以利用 find() 来查找子字符串在给定字符串中出现的总次数。
» 了解更多关于 Python 字符串 split()