Python Forum

Full Version: String Method 'find(...)'.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have a question about the string method 'find(...)'.

Consider the string 'tomato potato gelato'. How would I frame an optional argument such that the indexing would only begin at 'gelato'? In general terms, what I am trying to learn here is how to read function descriptions, because I don't get how the optional arguments work on the basis of the following description.


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. 
Thanks
Output:
mississippi ^ ^ 3 6
>>> 'mississippi'.find('si')
3
>>> 'mississippi'.find('si', 5)
6
Do you know why the following method call is returning this?

>>> 'si si si no yes si'.find('i', 6, 10)
7
I want it to return in failure, i.e. -1
for index, ch in enumerate('si si si no yes si'):
    print(f'{index} --> {ch}')
print('si si si no yes si'.find('i', 8, 10))
Output:
0 --> s 1 --> i 2 --> 3 --> s 4 --> i 5 --> 6 --> s 7 --> i 8 --> 9 --> n 10 --> o 11 --> 12 --> y 13 --> e 14 --> s 15 --> 16 --> s 17 --> i -1 >>>
as you can see, 'i' is on index 1, 4, 7 and 17. with start=6 and end=10 you catch the 'i' on index 7