C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: With the second call to find, please notice the argument "i + 1". This is where the search begins.
Info: The values returned are 4 and 19. Only the first letter index is returned, not a range.
Note: If you count the characters in the string (starting at 0), you will see that the P letters are located in those positions.
Python program that uses string find
value = "cat picture is cat picture"
# Find first index of this string.
i = value.find("picture")
print(i)
# Find first index (of this string) after previous index.
b = value.find("picture", i + 1)
print(b)
Output
4
19
Here: The string "python" is not found within the string. The value of the variable i is thus equal to negative one.
Python program that tests find result
value = "ralph waldo emerson"
i = value.find("python")
if i != -1:
# Not reached.
print("String found")
else:
print("String not found")
Output
String not found
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.
Python program that uses string find, while
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)
Output
4
19
Please note: The integer arguments are a range. We specify the first index and the last index.
Here: In this example, we call rfind twice. In the second call, we specify two range arguments.
Tip: This is like a slice we search. We stop searching one index before the location of the first instance.
Python program that uses string rfind
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 four words.
b = value.rfind("picture", 0, i - 1)
print(b)
Output
19
4
And: We adjust the end index, but leave the first index set to 0. Thus we iterate over matched substrings from the right.
Python program that uses rfind, while
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)
Output
19
4
Note: In most programs, checking for negative one is better. Avoiding exceptions improves performance.
Rindex: As with find and rfind, there is an rindex method available. This searches from the right, not the left.
Python program that uses string index
value = "abc def"
# Use index method.
i = value.index("def")
print(i)
# This causes an exception.
b = value.index("xyz")
Output
4
Traceback (most recent call last):
File "C:\programs\file.py", line 11, in <module>
b = value.index("xyz")
ValueError: substring not found
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).
Python program that uses in and not in on strings
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")
Output
Is PNG image
Is NOT JPG image
Indexes: The second and third argument to count() are the first index, and last index we are searching.
And: The string must be contained completely within those indexes to be counted.
Note: From my testing, the substring must be fully within the indexes to be matched.
Python program that uses count
value = "finnegans wake"
# Count this substring.
print(value.count("n"))
# Count substring in indexes 0 to 6.
print(value.count("n", 0, 6))
Output
3
2