-1

I have multiple similar looking strings like this:

s1 = 'carrots12509600apple2950950'
s2 = 'zebra77899967apple5557880'

I want to find a way to index the starting point of the word apple, meaning I want to find the n which returns:

s1[n] = a

Given that all strings have the word apple, and it is between two numbers, is there a way to find the starting point of the substring 'apple' in each string? The length of numbers before and after the word apple is also the same for each string, but not the character before the number before apple.

halo09876
  • 2,725
  • 12
  • 51
  • 71
  • Possible duplicate of [Python - Locating the position of a regex match in a string?](http://stackoverflow.com/questions/2674391/python-locating-the-position-of-a-regex-match-in-a-string) – Colonel Beauvel May 05 '17 at 13:46
  • [`s1.index('apple')`](https://docs.python.org/2/library/stdtypes.html#str.index) or [`s1.find('apple')`](https://docs.python.org/2/library/stdtypes.html#str.find) – khelwood May 05 '17 at 13:47

1 Answers1

4
'zebra77899967apple5557880'.index('apple')

or

'zebra77899967apple5557880'.find('apple')

index raises ValueError if the sub string does not exist in the string. find returns -1

Henry Heath
  • 1,072
  • 11
  • 19