Python字符串搜索并返回子字符串位置

# 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

转自:http://www.pulog.org/code/460/python-str-search/

你可能感兴趣的:(String,python,search)