0

I am trying to find the indices of a repetitive word in a string without whitespaces.

let's say the string is:

x = "catdogmousecathorsecatdog"

I am trying to find the index of where the word 'cat' is so the output should be 0, 11, 19

kiranr
  • 2,087
  • 14
  • 32
kim
  • 17
  • 3
  • one way (I'm pretty sure that wouldn't be the best way) to do that would be to check for every 'c' letter in string and when the letter 'c' is found to check whether the next 2 letters after that 'c' are "at" and if that is true to print the index of 'c' that was found (If explanation is not enough I can write some example code for that task) – icecube Apr 04 '21 at 17:22

1 Answers1

1

try this,

>>> string = "catdogmousecathorsecatdog"
>>> [i for i in range(len(string)) if string.startswith('cat', i)]
[0, 11, 19]
kiranr
  • 2,087
  • 14
  • 32