Finding the Last Occurrence: Using Python’s rindex to Return the Highest Substring Index

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.

💡 Problem Formulation: In Python, finding the last occurrence of a substring within a string is a common task. For instance, you might want to find the last position of the substring "apple" in the string "apple pie, apple jam, apple". The desired output in this case would be the index 28, signifying the start of the last occurrence of "apple".

Method 1: Using the rindex() Method

The rindex() method in Python is designed to find the highest index of a substring within a string. If the substring is found, it returns the index of the first character of the last occurrence of the substring. If the substring is not found, it raises a ValueError.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.
text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rindex(substring)
print(index)

Output: 28

This code snippet finds the last occurrence of the substring "apple" within the string. The rindex() method successfully returns the index 28, which is the starting index of the last “apple” in the string.

Method 2: Using str.rfind()

The rfind() method is similar to rindex(), but it returns -1 if the substring is not found, rather than raising an exception. This can be useful if you want to avoid handling exceptions and instead check for the -1 return value.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = text.rfind(substring)
print(index)

Output: 28

This example uses the rfind() method to locate the highest index of the substring "apple". In this scenario, it behaves similarly to rindex(), returning 28.

Method 3: Using regex with re.finditer()

When dealing with complex patterns, Python’s regex module can be used to find the last occurrence of a substring. The finditer() function returns an iterator that provides match objects. The last match object contains information about the last occurrence.

Here’s an example:

import re

text = "apple pie, apple jam, apple"
substring = "apple"
matches = list(re.finditer(substring, text))
if matches:
    index = matches[-1].start()
    print(index)

Output: 28

After converting the iterator to a list, we access the last match object to get the start index of the last occurrence of "apple". This method offers more flexibility in terms of searching patterns rather than fixed strings.

Method 4: Looping Backwards

If you prefer not to use built-in methods, you can also manually loop through the string backwards, looking for the substring. While less efficient, it’s a way to understand what’s happening under the hood, and it doesn’t require any special methods.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"

index = -1
for i in range(len(text) - len(substring), -1, -1):
    if text[i:i+len(substring)] == substring:
        index = i
        break

print(index)

Output: 28

The loop starts from the end of the string and searches for the substring. When it’s found, the loop breaks and outputs the starting index of the substring.

Bonus One-Liner Method 5: Using List Comprehension and rindex()

You can combine list comprehension with rindex() to find the last occurrence of a substring in a one-liner. While compact, this method uses more memory as it generates a list of indices.

Here’s an example:

text = "apple pie, apple jam, apple"
substring = "apple"
index = [i for i in range(len(text)) if text.startswith(substring, i)][-1]
print(index)

Output: 28

This one-liner creates a list of starting indices for each occurrence of the substring and then selects the last one. It’s a concise method but can be inefficient due to list creation.

Summary/Discussion

  • Method 1: rindex(). Direct and simple. However, error handling is needed if there’s a chance the substring isn’t present.
  • Method 2: rfind(). Similar to rindex() but returns -1 instead of raising exceptions. A good choice for a more graceful failure.
  • Method 3: Regex with re.finditer(). Powerful for pattern matching, can handle more complex searches than just fixed strings.
  • Method 4: Looping backwards manually. Educational but not efficient. Offers full control over the search process.
  • Method 5: List comprehension with rindex(). Elegant one-liner but may not be memory efficient.