python中返回字符串中指定字符的索引

1、返回第一个出现的索引

>>> test1
'absacaxy'
>>> test1.find("a")
0

2、返回特定字符的所有索引

>>> test1
'absacaxy'
>>> b = test1.count("a")
>>> c = -1
>>> for i in range(b):
    c = test1.find("a",c + 1,len(test1))
    print(c)

    
0
3
5
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14724830.html