-1

I'm working on a program in which I need the index position of a character that is present more than once in a string. I tried using the rfind method but it only returns 1 position(the highest index).

CODE:

word = "SYNONYMS"

I want to get the index values of the character 'S' in this word. Required output: [0, 7]

Thank you for the help in advance.

codester_09
  • 5,622
  • 2
  • 5
  • 27

1 Answers1

0

Try this.

word = "SYNONYMS"

indexLst = []
letter = 'S'

i = 0
while i<len(word)-1:
    try:
        idx = word.index(letter,i)
        indexLst.append(idx)
        i = idx+1
    except ValueError:
        break


print(indexLst) # [0, 7]

codester_09
  • 5,622
  • 2
  • 5
  • 27