Python Forum
String Method 'find(...)'. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: String Method 'find(...)'. (/thread-16393.html)



String Method 'find(...)'. - ClassicalSoul - Feb-26-2019

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


RE: String Method 'find(...)'. - buran - Feb-26-2019

Output:
mississippi ^ ^ 3 6
>>> 'mississippi'.find('si')
3
>>> 'mississippi'.find('si', 5)
6



RE: String Method 'find(...)'. - ClassicalSoul - Feb-27-2019

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


RE: String Method 'find(...)'. - buran - Feb-27-2019

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