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
>>> '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) 7I 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)) 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
|