700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python字符串查找位置_python中怎么返回指定查找字符的位置

python字符串查找位置_python中怎么返回指定查找字符的位置

时间:2019-06-29 23:46:06

相关推荐

python字符串查找位置_python中怎么返回指定查找字符的位置

展开全部

Python编程中对字符串进行搜索查找,并返回字符位e69da5e887aa62616964757a686964616f31333337396231置,案例代码如下:# multiple searches of a string for a substring

# using s.find(sub[ ,start[, end]])

#以下面test这段文本为例

text = 'MSKSASPKEPEQLRKLFIGGLSFETTDESLRSAHFESSSYGSAGRRF'

##查找上面文本中的SA字符串

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。