How do you find all occurrences of a character in a string python?

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

asked Oct 22, 2012 at 10:39

2

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.

How do you find all occurrences of a character in a string python?

Mooncrater

3,5544 gold badges27 silver badges53 bronze badges

answered Oct 22, 2012 at 10:50

Marco L.Marco L.

1,4794 gold badges17 silver badges25 bronze badges

3

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]

How do you find all occurrences of a character in a string python?

answered Oct 22, 2012 at 10:45

avasalavasal

13.9k4 gold badges30 silver badges47 bronze badges

It is easier to use regular expressions here;

import re

def findSectionOffsets(text):
    for m in re.finditer('\|', text):
        print m.start(0)

answered Oct 22, 2012 at 10:56

How do you find all occurrences of a character in a string python?

Roland SmithRoland Smith

40.8k3 gold badges59 silver badges87 bronze badges

import re
def findSectionOffsets(text)
    for i,m in enumerate(re.finditer('\|',text)) :
        print i, m.start(), m.end()

dbr

161k65 gold badges273 silver badges339 bronze badges

answered Oct 22, 2012 at 10:57

How do you find all occurrences of a character in a string python?

ZuluZulu

8,0899 gold badges46 silver badges55 bronze badges

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

How do you find all occurrences of a character in a string python?

answered Oct 22, 2012 at 10:48

How do you find all occurrences of a character in a string python?

samfrancessamfrances

2,9753 gold badges24 silver badges36 bronze badges

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.

answered Aug 30, 2018 at 7:26

How do you find all occurrences of a character in a string python?

Pablo ReyesPablo Reyes

2,92919 silver badges29 bronze badges

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)

answered Oct 22, 2012 at 10:48

How do you find all occurrences of a character in a string python?

M SomervilleM Somerville

4,33727 silver badges37 bronze badges

2

How do you find the occurrences of a string in a string Python?

The count() method returns the number of occurrences of a substring in the given string..
substring - string whose count is to be found..
start (Optional) - starting index within the string where search starts..
end (Optional) - ending index within the string where search ends..

How do you find the occurrence of all characters in a string?

In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.

How do you find the index of occurrences of a character in a string in Python?

5 Ways to Find the Index of a Substring in Strings in Python.
str.find().
str.rfind().
str.index().
str.rindex().
re.search().