How do you find all occurrences of a character in a string in 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 in python?

Mooncrater

3,5444 gold badges26 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 in python?

answered Oct 22, 2012 at 10:45

avasalavasal

13.8k4 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 in python?

Roland SmithRoland Smith

40.6k3 gold badges58 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 in python?

ZuluZulu

8,0599 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 in python?

answered Oct 22, 2012 at 10:48

How do you find all occurrences of a character in a string in 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 in 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 in 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 all instances of a character in a string?

1. Using indexOf() and lastIndexOf() method. The String class provides an indexOf() method that returns the index of the first appearance of a character in a string. To get the indices of all occurrences of a character in a String, you can repeatedly call the indexOf() method within a loop.

Which method finds the list of all occurrences of the pattern in the given string?

finditer() To get all occurrences of a pattern in a given string, you can use the regular expression method re. finditer(pattern, string) . The result is an iterable of match objects—you can retrieve the indices of the match using the match.

How do you count repeated letters in Python?

Python.
string = "Great responsibility";.
print("Duplicate characters in a given string: ");.
#Counts each character present in the string..
for i in range(0, len(string)):.
count = 1;.
for j in range(i+1, len(string)):.
if(string[i] == string[j] and string[i] != ' '):.
count = count + 1;.