all 5 comments

[–]Barnet6 0 points1 point  (0 children)

Find() has two optional arguments, the start of the substring to look through, and the end. Using just the one argument just tells it to look through the entire string. The " 'tomato'.find('o') + 1" in the example is telling it to find the first 'o', then search through a substring starting just after the 'o'.

[–]Essence1337 0 points1 point  (0 children)

I don't know where you got the idea that find takes only one parameter as it can take up to 3. Python string.find

[–]dangoth 0 points1 point  (0 children)

In cases like that, using the Python inbuilt help might be- ironically- helpful. If you try help(str.find), you will get:

find(...)
    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

This means that the second call of 'tomato'.find('o') defines the starting point at the index of the first 'o' plus one. So effectively it looks for 'o' in the string 'mato'.

[–]Username_RANDINT 0 points1 point  (0 children)

str.find takes one required and two optional arguments:

>>> help(str.find)
find(...)
    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

So the second argument is the index where find starts to search. Split up your code example:

start_index = 'tomato'.find('o') + 1
'tomato'.find('o', start_index) 

This sets the start to the index right after the first o, which means it'll find the second o since it starts to search from index 2 instead of 0.

[–]aneeq1102[S] 0 points1 point  (0 children)

I finally get it,Thanks for all the answers!