Yes,
Firstly and the root of your issue here, it doesn't know which occurrence of the character you are looking for so it just returns the index of the first location both times.
Secondly (and probably a little beyond your current knowledge), it is unnecessarily increasing the time complexity of your code. You are running in O(n^2) time for an algorithm that should be O(n).
What you should do is use enumerate.
Observe how I have access to the character and index of each character as I itterate and try to apply that to your issue:
str.index
is the wrong tool here for a few reasons.Firstly and the root of your issue here, it doesn't know which occurrence of the character you are looking for so it just returns the index of the first location both times.
Secondly (and probably a little beyond your current knowledge), it is unnecessarily increasing the time complexity of your code. You are running in O(n^2) time for an algorithm that should be O(n).
What you should do is use enumerate.
Observe how I have access to the character and index of each character as I itterate and try to apply that to your issue:
>>> my_string = "Spam and eggs" >>> >>> for i,char in enumerate(my_string): ... print(i, char) ... (0, 'S') (1, 'p') (2, 'a') (3, 'm') (4, ' ') (5, 'a') (6, 'n') (7, 'd') (8, ' ') (9, 'e') (10, 'g') (11, 'g') (12, 's') >>>