0

I need to know how can I find ALL the position of a group of letters in a string. For example, in the string "Canyoucanacanasacannercancanacan" the letters "an" recur 7 times. I want to know the exact position of each one, possibly in a list. How can I do?

Thank you!

4 Answers4

2

I would use re.finditer(), like so:

import re

s = "Canyoucanacanasacannercancanacan"
pattern = "an"

result = [m.start() for m in re.finditer(pattern, s)]

assert result == [1, 7, 11, 17, 23, 26, 30]

Note that this only finds the non-overlapping instances, which in your specific case is all of them.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

Surprisingly I couldn't find a duplicate of this question! You can do this using str.index, while updating the starting position that you're looking from to exclude parts of the string you've already checked.

s = 'Canyoucanacanasacannercancanacan'
position_list = []
i = 0
while True:
    try:
        position = s.index('an', i)
    except ValueError:
        break
    position_list.append(position)
    i = position + 1
print(position_list)

As someone suggested, you could also use str.find, like this:

s = 'Canyoucanacanasacannercancanacan'
position_list = []
i = s.find('an')
while i != -1:
    position_list.append(i)
    i = s.find('an', i+1)
print(position_list)
Jeremy McGibbon
  • 3,527
  • 14
  • 22
0

Try the following, you can modify the output as you wish (start and end):

import re

text = "Canyoucanacanasacannercancanacan"
for m in re.finditer(r"an", text):
    print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))

The output am getting:

01-03: an
07-09: an
11-13: an
17-19: an
23-25: an
26-28: an
30-32: an
aspo
  • 374
  • 2
  • 9
0

You can create a small generator to find all positions of letters in a text:

def find_positions(text, letters):
    curr = text.find(letters)
    while curr >= 0:
        yield curr
        curr = text.find(letters, curr + len(letters))

Usage:

positions = list(find_positions("Canyoucanacanasacannercancanacan", "an"))
print(positions)

You get:

[1, 7, 11, 17, 23, 26, 30]
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103