Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Method 'find(...)'.
#1
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
Reply
#2
Output:
mississippi ^ ^ 3 6
>>> 'mississippi'.find('si')
3
>>> 'mississippi'.find('si', 5)
6
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find and Replace numbers in String giddyhead 2 1,194 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Find string between two substrings, in a stream of data xbit 1 2,113 May-09-2021, 03:32 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,364 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  find a string in a field in MongoDB Leon79 2 2,371 Jul-19-2020, 09:20 PM
Last Post: menator01
  Help to find a string and read the next lines crlamaral 4 2,398 Mar-19-2020, 09:24 AM
Last Post: Larz60+
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 1,988 Mar-03-2020, 04:47 AM
Last Post: davidshq
  How to run a method on an argument in a formatted string Exsul 1 1,646 Aug-30-2019, 01:57 AM
Last Post: Exsul
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 2,903 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92
  Find string and add character - newbi PyDK 1 2,030 May-15-2019, 01:22 PM
Last Post: ichabod801
  How can I find a string in sequence (with Booleans)? go127a 3 2,191 Apr-23-2019, 01:58 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020