我无法使用索引找到超过 1 个字母

I can't find a letter more than 1 using index

我在玩索引,确实找到了字母的位置

sentence = "Coding is hard"

index = sentence.index("i")
print(index)

对我来说效果很好,但是当我想查找的不仅仅是 1 个时,它就不起作用了?

sentence = "Coding is hard"

index = sentence.index("i", index + 1) 
print(index)

没用?有人可以解释一下吗?

虽然 index() 是一种方法,并且评论者已经为您提供了帮助的指示,但另一种查找字符串中所有出现的字符的方法是使用列表理解:

sentence = "Coding is hard"
indices = [i for i, c in enumerate(sentence) if c == "i"]
print(indices)

这会打印:

[3, 7]