龙空技术网

DAY3-step9 Python字符串find()方法

易小盟办公设备企业店 43

前言:

当前你们对“python中find函数”大致比较重视,我们都想要了解一些“python中find函数”的相关文章。那么小编在网络上搜集了一些关于“python中find函数””的相关资讯,希望你们能喜欢,咱们一起来学习一下吧!

什么是find()

Python字符串find()方法有助于查找给定字符串中子字符串首次出现的索引。 如果子字符串不存在,它将返回-1。

语法

string.find(substring,start,end)
参数

substring:要在给定字符串中搜索的子字符串。

start :(可选)从开始搜索子字符串的起始值。 默认情况下为0。

end :(可选)搜索子字符串将结束的结束值。 默认情况下,该值是字符串的长度。

使用带有默认值的find()方法

传递给find()方法的参数是子字符串,即您要搜索的字符串。 默认情况下,起始值为0,终止值为字符串的长度。

在此示例中,我们将使用带有默认值的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
在find()中使用开始参数

您可以搜索给定字符串中的子字符串,并指定开始搜索的起始位置。

该示例将开始位置指定为20,find()方法将从位置20开始搜索。此处,结束位置将是字符串的长度,并将从20个位置开始搜索直到字符串的结尾。

例:

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))

Output:

The position of Tutorials is at 12
找不到时,返回-1

我们知道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))

Output:

The position of Best site is at: 27The position of Guru99 is at: -1
Python字符串rfind()

Python函数rfind()是从右向左查找。 如果子字符串不存在,则rfind()返回-1。

在下面的示例中,我们有一个字符串 "Meet Guru99 Tutorials Site. Best site for Python Tutorials!"。 并尝试使用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"))

Output:

The position of Tutorials using find() :  12The position of Tutorials using rfind() :  48

输出显示find()给出了它获得的第一个Tutorials子字符串的第一个字符的索引,rfind()给出最后出现的字符串Tutorials的第一个字符的索引。

Python字符串index()

Python字符串index()是一个函数,该函数将为您提供子字符串的位置,就像find()一样。 两者之间的唯一区别是,如果字符串中不存在子字符串,index()将引发异常,而find()将返回-1。

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() :  12The 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"))

Output:

The position of Tutorials using find() :  -1Traceback (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(),它会引发如上所示的错误。

查找子字符串的全部出现

my_string = "test string test, test string testing, test string test string"startIndex = 0count = 0for i in range(len(my_string)): #从0到字符串的len进行循环    k = my_string.find('test', startIndex)  #从startIndex开始查找    if(k != -1):  #如果找到        startIndex = k+1  #从找到的位置后一个位置重新找下一个        count += 1   #找到的数+1        k = 0   #复位是否找到的k变量print("The total count of substring test is: ", count )

Output:

The total count of substring test is:  6
摘要

Python字符串find()方法有助于查找给定字符串中子字符串首次出现的索引。如果子字符串不存在,它将返回-1。

传递给find()方法的参数是子字符串,即您要搜索的字符串。默认情况下,起始值为0,终止值为字符串的长度。

您可以搜索给定字符串中的子字符串,并指定开始搜索的起始位置。

使用start和end参数,我们将尝试限制搜索,而不是搜索整个字符串。

Python函数rfind()与find()函数相似,唯一的区别是rfind()是从右向左找。如果子字符串不存在,则rfind()和find()都将返回-1。

Python字符串index()是另一个函数,它将为您提供子字符串的位置,就像find()一样。两者之间的唯一区别是,如果字符串中不存在子字符串,index()将引发异常,而find()将返回-1。

标签: #python中find函数