How to find the position of the first occurrence of a substring in a string with Python

1 Answer

0 votes
s = "I bought running shoes, but they started running alone, now we are both happy"
substring = "running"

try:
    position = s.index(substring)
    print("The position of the first occurrence is:", position)
except ValueError:
    print("Substring not found")



'''
run:

The position of the first occurrence is: 9

'''

 



answered Apr 20 by avibootz
...