How to find the index of first appearance of a word in string with Python

3 Answers

0 votes
s = 'python php java php python'

i = s.find('php')

print(i)

'''
run:

7
  
'''

 



answered Aug 30, 2018 by avibootz
0 votes
import re

s = 'python php java php python'

i = re.search(r'\b(java)\b', s)

print(i.start())

'''
run:

11
  
'''

 



answered Aug 30, 2018 by avibootz
0 votes
import re

s = 'python php java php python'

i = re.search(r'\b( java )\b', s)

print(i.start())

'''
run:

10

'''

 



answered Aug 30, 2018 by avibootz
...