如何跳过python中的角色



只是想知道是否有一种简单的方法在使用.find()时在字符串中跳过字母例如,我在单词中寻找某个字母,也许是o比例。比例(o)我已经找到了第一个O的位置。我如何找到第二或第三个O的位置,而不必从单词中删除第一个o?

string = 'helloworld'
selectedCharacter = 'o'
print [position for position, char in enumerate(string) if char == selectedCharacter]

这将为您提供所选字符索引的列表(在这种情况下,字符'o',将为您提供[4, 6]

最新更新