36

I am trying to find all the occurences of "|" in a string.

def findSectionOffsets(text):
    startingPos = 0
    endPos = len(text)

    for position in text.find("|",startingPos, endPos):
        print position
        endPos = position

But I get an error:

    for position in text.find("|",startingPos, endPos):
TypeError: 'int' object is not iterable
theAlse
  • 5,417
  • 11
  • 67
  • 107
  • 4
    `text.find("|",startingPos, endPos)` is giving you a single `int` value, which the for loop is iterating on... – avasal Oct 22 '12 at 10:40
  • http://stackoverflow.com/questions/4664850/find-all-occurrences-of-a-substring-in-python – georg Oct 22 '12 at 10:57

7 Answers7

63

The function:

def findOccurrences(s, ch):
    return [i for i, letter in enumerate(s) if letter == ch]


findOccurrences(yourString, '|')

will return a list of the indices of yourString in which the | occur.

Mooncrater
  • 3,308
  • 4
  • 25
  • 51
Marco L.
  • 1,459
  • 4
  • 17
  • 24
  • how does that loop work? `i for i, letter in enumerate(s) if letter == ch`. – Goodword Jun 22 '15 at 23:04
  • @Goodword:This answer uses a *list comprehension*; see the [Python tutorial](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions). – Martijn Pieters Jun 01 '16 at 23:43
  • 3
    The function is spelled wrong on the definition line. It should be "def findOccurrences" with two r's. – R. Wayne Feb 04 '18 at 00:20
13

if you want index of all occurrences of | character in a string you can do this

import re
str = "aaaaaa|bbbbbb|ccccc|dddd"
indexes = [x.start() for x in re.finditer('\|', str)]
print(indexes) # <-- [6, 13, 19]

also you can do

indexes = [x for x, v in enumerate(str) if v == '|']
print(indexes) # <-- [6, 13, 19]
Hussein Esmail
  • 329
  • 5
  • 18
avasal
  • 13,666
  • 4
  • 29
  • 47
2

It is easier to use regular expressions here;

import re

def findSectionOffsets(text):
    for m in re.finditer('\|', text):
        print m.start(0)
Roland Smith
  • 39,308
  • 3
  • 57
  • 86
2
import re
def findSectionOffsets(text)
    for i,m in enumerate(re.finditer('\|',text)) :
        print i, m.start(), m.end()
dbr
  • 159,759
  • 65
  • 272
  • 339
Zulu
  • 7,776
  • 9
  • 44
  • 55
1

text.find returns an integer (the index at which the desired string is found), so you can run for loop over it.

I suggest:

def findSectionOffsets(text):
    indexes = []
    startposition = 0

    while True:
        i = text.find("|", startposition)
        if i == -1: break
        indexes.append(i)
        startposition = i + 1

    return indexes
M.Shuaib Imran
  • 1,267
  • 16
  • 29
samfrances
  • 2,785
  • 2
  • 22
  • 35
1

text.find() only returns the first result, and then you need to set the new starting position based on that. So like this:

def findSectionOffsets(text):
    startingPos = 0

    position = text.find("|", startingPos):
    while position > -1:
        print position
        startingPos = position + 1
        position = text.find("|", startingPos)
M Somerville
  • 4,288
  • 27
  • 37
1

If text is the string that you want to count how many "|" it contains, the following line of code returns the count:

len(text.split("|"))-1

Note: This will also work for searching sub-strings.

Pablo Reyes
  • 2,899
  • 18
  • 29