HOWTO · Python
How to Find Character in a String in Python
Discover the Pythonic way to locate a character's position within a string with our guide. Learn efficient techniques for finding the position of a character in Python strings.
On this page
Python string indices start at 0. To find a character or substring, use find() for its first match, rfind() for its last match, or index() when a missing match should raise ValueError. Both find() and rfind() return -1 when there is no match. To collect every matching index, iterate with enumerate(). The code examples below were executed in the supplied Python runtime, and their observed results are described in each section.
These methods search strings and are case-sensitive. Each accepts the value to search for and optional start and end bounds: start is included and end is excluded. For example, s.find("n", 6, 13) searches only the slice from index 6 through index 12. The result is an index measured from the beginning of the original string, not a position relative to the searched slice.
Using the find() method to find the first match and handle a missing match
Call find() on the string, passing the character or substring you want to locate. It returns the index of the first match from left to right. If the value is absent, it returns -1, so check for that result before using it as an index.
For s = "python is fun" and c = "n", s.find(c) returns 5: the first n is at zero-based index 5. The method’s general form is string.find(substring, start, end); omit the optional bounds to search the whole string.
Here is a complete example for the first match:
s = "python is fun"
c = "n"
print(s.find(c))
The example searches from the start of s. Its printed result is 5. For a lookup that might not find a match, a condition such as position = s.find(c) followed by if position != -1: lets your code handle absence explicitly.
Using the rfind() method to find the last match
Use rfind() when you need the last occurrence of a character or substring. It searches from the right and returns the highest matching index, while the index itself is still counted from the left. Like find(), it accepts optional start and end bounds and returns -1 when there is no match.
For the same string and character, s.rfind(c) returns 12, the index of the last n in "python is fun". Its general form is string.rfind(substring, start, end).
s = "python is fun"
c = "n"
print(s.rfind(c))
Here the result is 12. Choose find() when the earliest match matters and rfind() when the latest match matters; neither method raises an error just because the search value is absent.
Using the index() method when a missing match should raise an error
index() also returns the first matching index and accepts the same optional start and end bounds. Its key difference from find() is its missing-match behavior: if the character or substring is absent, index() raises ValueError instead of returning -1. Use it when absence should be treated as an error; use find() when you want to check for absence with a return value.
With s = "python is fun" and c = "n", s.index(c) returns 5. The general form is string.index(substring, start, end).
s = "python is fun"
c = "n"
print(s.index(c))
This example searches for the first n, so its result is 5. Do not describe the missing case as index() returning ValueError: the method raises that exception, and callers can handle it with a try/except statement if appropriate.
Using a for loop with enumerate() to find every matching index
find(), rfind(), and index() each return one matching index. To find every occurrence, loop over the string with enumerate(), which pairs each character with its zero-based index. Append the index whenever the character matches the value you are searching for.
s = "python is fun"
c = "n"
lst = []
for pos, char in enumerate(s):
if char == c:
lst.append(pos)
print(lst)
For this string, the matching indices are [5, 12]: the two n characters occur at indices 5 and 12. The same result can be expressed with the inline list-comprehension form [pos for pos, char in enumerate(s) if char == c]. This keeps the iteration and match condition together when a separate loop body is unnecessary.
Both approaches inspect the characters in order and use the same zero-based indices. Choose the loop when you want to do more work for each match; choose the list comprehension when collecting the matching indices is the whole task.