如何在python字符串中找到第一个出现的子字符串?

时间:2022-09-13 07:48:15

So if my string is "the dude is a cool dude".
I'd like to find the first index of 'dude':

所以,如果我的字符串是“老兄是一个很酷的家伙”。我想找到'dude'的第一个索引:

mystring.findfirstindex('dude') # should return 4

What is the python command for this?
Thanks.

这个python命令是什么?谢谢。

2 个解决方案

#1


133  

find()

找()

>>> s = "the dude is a cool dude"
>>> s.find('dude')
4

#2


10  

Quick Overview: index and find

Next to the find method there is as well index. find and index both yield the same result: returning the position of the first occurrence, but if nothing is found index will raise a ValueError whereas find returns -1. Speedwise, both have the same benchmark results.

在find方法旁边还有索引。 find和index都产生相同的结果:返回第一次出现的位置,但如果没有找到,index将引发ValueError而find返回-1。 Speedwise,两者都有相同的基准测试结果。

s.find(t)    #returns: -1, or index where t starts in s
s.index(t)   #returns: Same as find, but raises ValueError if t is not in s

Additional knowledge: rfind and rindex:

In general, find and index return the smallest index where the passed-in string starts, and rfind and rindex return the largest index where it starts Most of the string searching algorithms search from left to right, so functions starting with r indicate that the search happens from right to left.

通常,find和index返回传入字符串开始的最小索引,rfind和rindex返回它开始的最大索引大多数字符串搜索算法从左到右搜索,因此以r开头的函数表示搜索从右到左发生。

So in case that the likelihood of the element you are searching is close to the end than to the start of the list, rfind or rindex would be faster.

因此,如果您搜索的元素的可能性接近结束而不是列表的开头,则rfind或rindex会更快。

s.rfind(t)   #returns: Same as find, but searched right to left
s.rindex(t)  #returns: Same as index, but searches right to left

Source: Python: Visual QuickStart Guide, Toby Donaldson

来源:Python:Visual QuickStart Guide,Toby Donaldson

#1


133  

find()

找()

>>> s = "the dude is a cool dude"
>>> s.find('dude')
4

#2


10  

Quick Overview: index and find

Next to the find method there is as well index. find and index both yield the same result: returning the position of the first occurrence, but if nothing is found index will raise a ValueError whereas find returns -1. Speedwise, both have the same benchmark results.

在find方法旁边还有索引。 find和index都产生相同的结果:返回第一次出现的位置,但如果没有找到,index将引发ValueError而find返回-1。 Speedwise,两者都有相同的基准测试结果。

s.find(t)    #returns: -1, or index where t starts in s
s.index(t)   #returns: Same as find, but raises ValueError if t is not in s

Additional knowledge: rfind and rindex:

In general, find and index return the smallest index where the passed-in string starts, and rfind and rindex return the largest index where it starts Most of the string searching algorithms search from left to right, so functions starting with r indicate that the search happens from right to left.

通常,find和index返回传入字符串开始的最小索引,rfind和rindex返回它开始的最大索引大多数字符串搜索算法从左到右搜索,因此以r开头的函数表示搜索从右到左发生。

So in case that the likelihood of the element you are searching is close to the end than to the start of the list, rfind or rindex would be faster.

因此,如果您搜索的元素的可能性接近结束而不是列表的开头,则rfind或rindex会更快。

s.rfind(t)   #returns: Same as find, but searched right to left
s.rindex(t)  #returns: Same as index, but searches right to left

Source: Python: Visual QuickStart Guide, Toby Donaldson

来源:Python:Visual QuickStart Guide,Toby Donaldson