# multiple searches of a string for a substring |
# using s.find(sub[ ,start[, end]]) |
|
text = 'MSKSASPKEPEQLRKLFIGGLSFETTDESLRSAHFESSSYGSAGRRF' |
|
search = 'SA' |
start = 0 |
while True: |
index = text.find(search, start) |
# if search string not found, find() returns -1 |
# search is complete, break out of the while loop |
if index == -1: |
break |
print( "%s found at index %d" % (search, index) ) |
# move to next possible start position |
start = index + 1 |
//结果:
SA found at index 3
SA found at index 31
SA found at index 41
本文详细介绍了如何在Python中使用find()函数进行字符串搜索,并通过示例代码展示了搜索多个子字符串的过程。

被折叠的 条评论
为什么被折叠?



