Bottom Page

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
Quote
#2
Output:
mississippi ^ ^ 3 6
>>> 'mississippi'.find('si')
3
>>> 'mississippi'.find('si', 5)
6
Quote
#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
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to run a method on an argument in a formatted string Exsul 1 104 Aug-30-2019, 01:57 AM
Last Post: Exsul
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 238 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92
  Find string and add character - newbi PyDK 1 225 May-15-2019, 01:22 PM
Last Post: ichabod801
  How can I find a string in sequence (with Booleans)? go127a 3 317 Apr-23-2019, 01:58 PM
Last Post: ichabod801
  How to use a string method on user input Exsul 2 409 Mar-17-2019, 08:12 PM
Last Post: Exsul
  why my method doesn't find my List in the same class? Scorpio 2 346 Jan-31-2019, 05:21 PM
Last Post: Scorpio
  Find ? in string MuntyScruntfundle 5 460 Jan-17-2019, 08:11 PM
Last Post: buran
  Find closest string pattern jmair 9 609 Dec-12-2018, 09:17 PM
Last Post: jmair
  "replace() method" fails to change string pw928gts 4 553 Nov-30-2018, 05:48 PM
Last Post: nilamo
  How to find previous string of a string Prince_Bhatia 1 605 Sep-26-2018, 02:31 PM
Last Post: gruntfutuk

Forum Jump:


Users browsing this thread: 1 Guest(s)