42

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,577
  • 11
  • 68
  • 110
  • 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

66

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
  • 4,146
  • 4
  • 33
  • 62
Marco L.
  • 1,489
  • 4
  • 17
  • 25
  • 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
18

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
  • 353
  • 5
  • 21
avasal
  • 14,350
  • 4
  • 31
  • 47
  • thanks. good answer - fyi, you shouldn't use `str` as a variable name. it overwrites the native function – Joe B Apr 28 '23 at 22:11
3
import re
def findSectionOffsets(text)
    for i,m in enumerate(re.finditer('\|',text)) :
        print i, m.start(), m.end()
dbr
  • 165,801
  • 69
  • 278
  • 343
Zulu
  • 8,765
  • 9
  • 49
  • 56
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
  • 42,427
  • 3
  • 64
  • 94
2

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
  • 3,073
  • 1
  • 20
  • 30
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,287
  • 16
  • 29
samfrances
  • 3,405
  • 3
  • 25
  • 40
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,499
  • 30
  • 38