Find all the occurrences of a character in a string

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.

Leave a Comment