如题: python如何获得多次出现的字符串在另一个字符串中的位置 python如何判断一个字符串是否在另一个字符串中 python如何获得一个字符串在另一个字符串中的首位置
1. 判断是str2是否在str1中
str1 = "iloveyou"
str2 = "you"
if str2 in str1:
print('yes')
2. 判断str2在str1中的首位置(唯一)
str1 = "iloveyou"
str2 = "you"
site = str1.index(str2)
print(site)
3. 判断str2在str1中的首位置(出现多个str2)
str1 = "iloveYou and You"
str2 = "You"
cur_list = []
last_cur = 0
while(1):
where=str1.find(str2)
if(not where==-1):
cur_list.append(last_cur + where)
str1=str1[where+len(str2):]
last_cur = last_cur + where + len(str2)
else:
break
print(cur_list)