Home
Search
Find String ExamplesCall the find, index and count methods: search strings in loops for substrings.
Python
This page was last reviewed on Oct 28, 2022.
Find, index. Often Python programs start by parsing strings. We need to locate specific regions of a string. The find() and index() functions are helpful here.
With find, and its friend rfind, we scan strings. If the substring is found, find() returns its index. If no match is located, it returns -1.
First example. Here we declare a string that has the substring "xy" in it twice. Then we invoke the find() method (which is like indexOf in other languages).
Part 1 We use find to locate "xy." If you count characters starting at 0, the string "xy" begins at index 1.
Part 2 With the second call to find, please notice the argument "i + 1". This is where the search begins.
Info The values returned are 1 and 4. Only the first letter index is returned, not a range.
value = "_xy_xy" # Part 1: find first index of this string. i = value.find("xy") print("RESULT:", i) # Part 2: find first index (of this string) after previous index. b = value.find("xy", i + 1) print("RESULT:", b)
RESULT: 1 RESULT: 4
Not found. Find returns -1 if no value is found. We must check this value, often in an if-statement, to detect when the string was not found. Usually we cannot just use the index directly.
Here The string "python" is not found within the string. The value of the variable "i" is thus equal to negative one.
value = "ralph waldo emerson" i = value.find("python") if i != -1: # Not reached. print("String found") else: print("String not found")
String not found
While. Suppose we want to loop over all instances of a string within another string. A while-loop with find can accomplish this. We use the result of find to advance the starting index.
While
Tip We could optimize this sample further. Try changing the second argument to find to add the length of string.
And This will avoid searching all the characters within a found substring. This avoids finding overlapping strings.
value = "cat picture is cat picture" # Start with this value. location = -1 # Loop while true. while True: # Advance location by 1. location = value.find("picture", location + 1) # Break if not found. if location == -1: break # Display result. print(location)
4 19
Rfind. This method searches from the right. It returns the index of the rightmost substring within the (optional) range specified.
value = "cat picture is cat picture" # Get rightmost index of this string. i = value.rfind("picture") print(i) # Get rightmost index within this range of characters. # ... We search the left 4 words. b = value.rfind("picture", 0, i - 1) print(b)
19 4
Rfind, loop. We can use the rfind method in a loop. Here we adjust the range of characters we search as we progress through the string.
And We adjust the end index, but leave the first index set to 0. Thus we iterate over matched substrings from the right.
value = "cat picture is cat picture" # Start with length of string. i = len(value) while True: # Find rightmost string in this range. i = value.rfind("picture", 0, i) # Check for not found. if i == -1: break print(i)
19 4
Index. This method is the same as find on strings, except for one big difference. Index() raises an error when the string is not found.
Note In most programs, calling find() and checking for -1 is better—avoiding exceptions improves performance.
value = "abc def" # Use index method. i = value.index("def") print(i) # This causes an exception. b = value.index("xyz")
4 Traceback (most recent call last): File "C:\programs\file.py", line 11, in <module> b = value.index("xyz") ValueError: substring not found
In-operator. This can also search strings. It returns no index. It simply returns True if the string is found in the source string, and False if not.
In
Note The in-operator has simpler syntax. It is often preferred if no index is required.
Here We use "in" and "not in" to see if a string contains certain file extensions (these may be anywhere in the string).
filename = "cat.png" # See if the string contains this substring. if ".png" in filename: print("Is PNG image") # This is evaluated to true. if ".jpg" not in filename: print("Is NOT JPG image")
Is PNG image Is NOT JPG image
Count. This Python method counts up matching substrings. The first argument is the substring we want to count, and the second 2 (optional) arguments are the range.
value = "finnegans wake" # Count this substring. print(value.count("n")) # Count substring in indexes 0 to 6. print(value.count("n", 0, 6))
3 2
A review. A string can be searched in many ways. With find, and its friend rfind, we get the index of a located match. With "in," we see if the string exists.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 28, 2022 (simplify).
Home
Changes
© 2007-2022 Sam Allen.