HOWTO · Python
How to Find All Indexes of a Character in Python String
We can use regular expressions, the yield keyword, and list comprehensions to find all the indexes of a character inside a string in Python.
On this page
To find every occurrence of a character in a Python string, pair enumerate() with a condition and collect the matching indexes. Python indexes start at 0, so the first character is at index 0. The examples below use the string "This is a string" and search for lowercase "i".
Find All Character Indexes With enumerate() and a List Comprehension
For a string and target character, this list comprehension returns every matching index in order. Run this example:
string = "This is a string"
char = "i"
indices = [index for index, character in enumerate(string) if character == char]
print(indices)
enumerate(string) supplies a pair for each character: its index and its value. The condition keeps an index only when that value equals char, and the list comprehension gathers those indexes into a new list. With the sample input, the matching positions are 2, 5, and 13. Matching is case-sensitive: searching for "I" would not match lowercase "i". If the target does not occur, the result is []. This approach is usually the clearest choice when the caller needs all indexes at once.
Generate Matching Indexes Lazily With yield
A generator is useful when results should be consumed one at a time or the input is large. The function below follows the same comparison and order as the list-comprehension approach, but yields each index as iteration reaches it.
def find_indexes(string, char):
for index, character in enumerate(string):
if character == char:
yield index
string = "This is a string"
char = "i"
indices = list(find_indexes(string, char))
print(indices)
Calling find_indexes(string, char) creates a generator; its body runs as values are requested. Use a for loop to process each index incrementally, or wrap the call in list(...) when a complete list is needed. Materializing the generator gives the same indexes as the list comprehension. If there are no matches, iteration produces no values and list(...) is []. The yield keyword pauses the function while it gives a value to its caller, then resumes at the next iteration.
Find All Character Indexes With Regular Expressions
For all three methods in this article, char must be a one-character Python string. This shared input assumption keeps the character-by-character comparisons and the regex example consistent. Use re.finditer() when the search is naturally described by a regular-expression pattern. Each match object provides its starting position through start(). When the target is a literal character supplied as data, pass it through re.escape(); otherwise characters such as ., *, or [ can be interpreted as regex syntax rather than searched literally.
import re
string = "This is a string"
char = "i"
pattern = re.escape(char)
indices = [match.start() for match in re.finditer(pattern, string)]
print(indices)
[2, 5, 13]
Escaping is important for a literal target, especially if it may contain regex metacharacters. If you intentionally want a pattern, pass that pattern directly to re.finditer() and do not escape it. For a single literal character, enumerate() is simpler; regex is most useful when the matching rule itself needs pattern features. All three approaches scan from left to right, preserve repeated matches, and return zero-based positions.