Python String find() 方法及示例
什么是 Python 字符串 find()?
Python 字符串查找() 是 Python 库中可用的函数,用于从给定字符串中查找子字符串第一次出现的索引。如果给定的字符串中不存在指定的子字符串,则字符串 find() 函数将返回 -1 而不是抛出异常。
在本 Python 字符串 find() 方法教程中,您将学习:
- 什么是 Python 字符串 find()?
- Python 字符串 find() 的语法
- 使用默认值的 find() 方法示例
- 使用 start 参数的 find() 示例
- 使用开始和结束参数的 find() 示例
- find() 方法示例 查找给定子字符串在字符串中的位置
- Python 字符串 rfind()
- Python 字符串索引()
- 查找子字符串的总出现次数
Python 字符串 find() 的语法
Python find()函数的基本语法如下:
string.find(substring,start,end)
find() 方法的参数
这里,是Python中函数String find()的三个参数:
- 子字符串 :要在给定字符串中搜索的子字符串。
- 开始 :(可选)开始搜索子字符串的起始值。默认为 0。
- 结束 :(可选)搜索子字符串的结束值。默认情况下,该值是字符串的长度。
使用默认值的 find() 方法示例
传递给 Python find() 方法的参数是子字符串,即您要搜索、开始和结束的字符串。起始值默认为0,结束值为字符串的长度。
在此示例中,我们将使用 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参数也可以这样用。
该示例将指定起始位置为 15,Python 方法中的 find() 方法将从位置 15 开始搜索。这里,结束位置将是字符串的长度,将从 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
使用开始和结束参数的 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。
在下面的示例中,我们有一个字符串“认识 Guru99 教程网站。 Python 教程的最佳网站!”并将尝试使用 find() 和 rfind() 找到子字符串教程的位置。字符串中出现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 字符串索引()
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 –loop 内部将检查子字符串是否存在于使用 find() 和 startIndex 为 0 给出的字符串中。
find() 返回的值如果不是 -1,则将 startIndex 更新为找到字符串的索引,并增加计数值。
这是工作示例:
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 方法的参数是 substring,即要搜索、开始和结束的字符串。起始值默认为 0,结束值为字符串的长度。
- 您可以搜索给定字符串中的子字符串,并指定开始位置,即开始搜索的位置。 start 参数也可以用来做同样的事情。
- 使用 start 和 end 参数,我们将尝试限制搜索,而不是搜索整个字符串。
- Python 函数 rfind() 类似于 find() 函数,唯一的区别是 rfind() 给出给定子字符串的最高索引,而 find() 给出最低索引,即第一个索引。如果子字符串不存在,rfind() 和 find() 都将返回 -1。
- Python 字符串 index() 是另一个函数,它可以像 find() 一样为您提供给定的子字符串的位置。两者之间的唯一区别是,如果子字符串不存在于字符串中,index() 将抛出异常,而 find() 将返回 -1。
- 我们可以使用 find() 来查找给定字符串中子字符串的总出现次数。
Python
- Java 中的 String Length() 方法:如何通过示例查找
- Java String indexOf() 方法与子字符串和示例
- Java String charAt() 方法及示例
- Python String strip() 函数与示例
- 带有示例的 Python 字符串计数()
- Python String format() 举例说明
- Python 字符串长度 | len() 方法示例
- 带有示例的 Python Lambda 函数
- 带有示例的 Python round() 函数
- 带有示例的 Python map() 函数
- Python Timeit() 与示例
- Python 中的 type() 和 isinstance() 示例