Below is the given list of strings from which I am trying to find the strings which contains the substring. Output is obtained perfectly without using functions. However when function is used, it does return only 1 substring and not the list of strings containing the substring.
L = ['dog', 'dodge', 'cat', 'cattle']
sub_string = str(input("Enter the substring: "))
for i in range(len(L)):
v = L[i].find(sub_string, 0,100)
if v >= 0:
print(L[i]) #returns all the strings containing the substring very well.
def String_find(sub_string):
for i in range(len(L)):
if sub_string in L[i]:
return L[i] #returns only 'dog' or 'cat' if substring is entered whereas
#'dog','dodge'
#or 'cat','cattle' is expected.
Total = String_find(sub_string)
#Output is obtained perfectly without using functions however when function is used it does return
#only 1 substring and not the list of strings containing the substring.
returnreturns immediately, so the first match will trigger the exit of the function. You may be lookiing foryield, or to return a list that you fill with matches.returnwill cause the loop to exit. So as soon as a match is found, it exits from the loop.