gpt4 book ai didi

python - 搜索字符串以查找索引

转载 作者:行者123 更新时间:2023-11-28 19:47:02 24 4
gpt4 key购买 nike

我想找到我的字符串中“a”项的所有索引,但它只打印第一个。这个程序有什么问题:

def str_lookup_reverse(s, c):
for index in range(len(s)-1):
if s[index] == c:
print(index)

s = "parisa"
str_lookup_reverse(s, "a")

最佳答案

你试过这样吗:

for i, letter in enumerate(s):
if letter == c:
print(i)

关于python - 搜索字符串以查找索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48166814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com