如何在python字符串中找到子字符串的首次出现?

问题:如何在python字符串中找到子字符串的首次出现?

因此,如果我的字符串是“花花公子很酷”。
我想找到’dude’的第一个索引:

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

这是什么python命令?
谢谢。

So if my string is “the dude is a cool dude”.
I’d like to find the first index of ‘dude’:

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

What is the python command for this?
Thanks.


回答 0

find()

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

find()

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

回答 1

快速概述: indexfind

find方法旁边也有indexfindindex这两个产生相同的结果:返回第一个出现的位置,如果没有找到index将引发ValueError,而find回报-1。在速度方面,两者都有相同的基准结果。

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

其他知识: rfindrindex

在一般情况下,发现和指数收益率,其中传入的字符串开始最小的指数,并rfindrindex返回它开始大部分的字符串搜索算法进行搜索的最大索引从左到右,所以开始的功能r表示搜索从发生右向左

因此,如果您正在搜索的元素的可能性比列表的开始更接近结尾,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

来源: Python:Visual快速入门指南,Toby Donaldson

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.

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.

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.

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


回答 2

通过不使用任何python内置函数来以算法方式实现此功能。这可以实现为

def find_pos(string,word):

    for i in range(len(string) - len(word)+1):
        if string[i:i+len(word)] == word:
            return i
    return 'Not Found'

string = "the dude is a cool dude"
word = 'dude1'
print(find_pos(string,word))
# output 4

to implement this in algorithmic way, by not using any python inbuilt function . This can be implemented as

def find_pos(string,word):

    for i in range(len(string) - len(word)+1):
        if string[i:i+len(word)] == word:
            return i
    return 'Not Found'

string = "the dude is a cool dude"
word = 'dude1'
print(find_pos(string,word))
# output 4

回答 3

def find_pos(chaine,x):

    for i in range(len(chaine)):
        if chaine[i] ==x :
            return 'yes',i 
    return 'no'
def find_pos(chaine,x):

    for i in range(len(chaine)):
        if chaine[i] ==x :
            return 'yes',i 
    return 'no'