Fix Python – How can I find the first occurrence of a sub-string in a python string?

Question

Asked By – Shai UI

Given the string “the dude is a cool dude”,
I’d like to find the first index of ‘dude’:

mystring.findfirstindex('dude') # should return 4

What is the python command for this?

Now we will see solution for issue: How can I find the first occurrence of a sub-string in a python string?


Answer

find()

>>> s = "the dude is a cool dude"
>>> s.find('dude')
4

This question is answered By – mechanical_meat

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0