Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Python string substr

The task is to find a the first string as a substring in the other string.
For example, let's say we have:
string 1 = 'bob'
string 2 = 'bobsbugsbegone'
for i in s2:
s1 == to [b]ONLY[/b] [0:2] of s2

I want to write a code that detects when there's a substring in a string and that counts how many substrings are found. (the second part is easy)
This is my code:

import sys

s1 = sys.argv[1]
s2 = sys.argv[2]
k = 0
i = 0

def substrings(s1, s2, k):
for k in s2:
for n in range(len(s2)):
if k == s1[n]:
return True
print('s1 is a substring in s2')
else:
print('s1 is not a substring in s2')
return False

print(substrings(s1, s2, k))

Help?

In addition to that:
In the first try it works
In the second try it also works
In the third try, i found out that it works only if the substring is at the beginning of the second string
by

0 Answers

No Answer posted yet.

Login / Signup to Answer the Question.