我正在尝试编写一个函数以在消息中查找字符串,但是在设置索引时遇到了麻烦。我希望我的函数查找给定消息中所有出现的字符串并打印。非常感谢您的帮助。谢谢!
代码:
def find_substr(mssg, search):
searchstr = str(search)
mssgstr = str(mssg)
ix = 0
a = []
found = False
while ix < len(mssgstr): # Condition to loop
if searchstr == mssgstr[]:
a.append(c)
found = True
ix = ix + 1
if found and c == len(mssgstr):
for i in a:
print(i)
else:
print(-1)
messag = " man man women women man"
find_substr(messag, "man")