Skip to main content
 首页 » 编程设计

python之如何查找字符串中某个字符所有出现的索引

2025年05月04日26qq号

<分区>

我想标题是不言自明的,不过,我会让自己更清楚。

我必须找到字符串中每个字符出现的索引。例如,

word = "banana" 
def indexes(x, word): 
    #some code 
    return (list of indexes of x character in the word) 

输出:

indexes('a', word) 
>> [1, 3, 5] 

我如何得到这个结果?