Find
, indexOften 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.
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).
string
"xy" begins at index 1.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
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.
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
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.
string
.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
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
We can use the rfind method in a loop. Here we adjust the range of characters we search as we progress through the string
.
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.
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
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
-operator has simpler syntax. It is often preferred if no index is required.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 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.