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
Post a Comment