-1

I am trying to iterate over a string and find all of the occurrences of a certain substring, like so:

contant = "old men old men old men"

def this (l,n):
  while n < 20:
    m = contant[l:].index("o")
    l = m + 3
    n += 1
    print(m,l)

this(0,1)

Which will only return the nnumbers 0 3 and 5 8 over and over again, instead of iterating through the entire string.

1 Answers1

0

Try this

value = "old men old men old men"

location = -1

# Loop while true.
while True:
    # Advance location by 1.
    location = value.find("o", location + 1)

    # Break if not found.
    if location == -1: break

    # Display result.
    print(location)

output

0
8
16
Ashfaque Ali Solangi
  • 1,883
  • 3
  • 22
  • 34