python - Checking whether string is a sub-string or not -


i trying check whether given string sub-string of string, , print index value of string sub-string takes place.

so far have code. reduce complexity using single loop.

s1 = "robot" s2 = "bot" in range(len(s2)):             if s1[i] == s2[i]:                =i +1              print(i) 

or alternatively thinking this,

for in range(len(s1)):     j in range(len(s2)):        if s1[i] == s2[j]                 = i+1                 j = j+1        print(i) 

i know can use in keyword solve this, want learn logic behind it.

please me. trying long time solve these.

you can use builtin string method find() return index of substring, or -1 if not found.

>>> "robot".find("bot") 2 >>> "robot".find("xbot") -1 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -