| by Arround The Web | No comments

Python Find Last Occurrence in String

Finding the last occurrence of a substring in a Python string is an important task when working with text data. The term “substring” refers to sequences of characters within a particular string. For example, in the string “Linux hint”, the sequence of characters “hint” corresponds to a substring. This Python tutorial shows you how to get the index of the last occurrence of a character in a Python string utilizing different methods.

How to Get/Find the Last Occurrence in String in Python?

There are many ways to determine the final/last occurrence of a string character in Python. There are several common methods, including:

Method 1: Applying the “rfind()” Method to Find Last Occurrence in String

The “rfind()” is a built-in method that is specifically designed to locate the last occurrence of a substring in a string.

Syntax

string.rfind(substring, start=0, end=None)

 

In the above syntax:

  • The “substring” parameter specifies the character or substring of which you want to locate the last occurrence.
  • The “start” parameter specifies the index from which to begin searching.
  • The “end” parameter specifies the index at which you want to stop/end searching. The search will go on until it reaches the string end unless you define the “end” parameter.

If the specified character or substring appears in the string, the “rfind()” method gives the index of its last occurrence. Otherwise, it gives “-1”.

Example

The following code will find the last occurrence of the character:

string_value = "Welcome to Python Guide"
print(string_value.rfind('e'))

 

In the above code, the “string.rfind()” method accepts the particular character as an argument and finds its last occurrence in the string.

Output

The last occurrence of the particular character in the defined string has been displayed in this snippet.

Method 2: Applying the “rindex()” Method to Find Last Occurrence in String

The “rindex()” is another built-in method that can be utilized to determine the last/final occurrence of a character/substring in a given string.

Syntax

string.rindex(value, start, end)

 

In the above syntax, “value” is the substring to be searched, and “start” and “end” are optional parameters that specify the range of the string to be searched.

Note: This method will raise an exception if the substring/ is not exists/found.

Example

This code finds the last occurrence of the character:

string_value = "Welcome to Python Guide"
print(string_value.rindex('e'))

 

The above code applies the “rindex()” method that takes the character as a parameter and retrieves the index of the last occurrence in the string.

Output

The above snippet displays the last occurrence of the targeted character.

Method 3: Applying the “str.rpartition()” Method to Find Last Occurrence in String

The “str.rpartition()” method can also be utilized to locate the last occurrence of a character/substring in a string.

Syntax

string.rpartition(substring)

 

In the above syntax, “substring” specifies the character or substring that needs to be evaluated for the last occurrence.

Return Value

The “str.rpartition()” method returns a tuple of three elements:

  • The first element is the part/piece of the string before the final/last occurrence of the input character.
  • The second element is the last occurrence of the input substring/character.
  • The third element is the string part after the last/final occurrence of the defined character.

Example

Here is the code that finds the last occurrence of the character:

string_value = "Welcome to Python Guide"
print(string_value.rpartition('e'))

 

In the above code, the “rpartition()” method takes the stated character as an argument and returns the tuple containing three elements: part of the string before the occurrence, the last occurrence of the specified character, and the value after the last occurrence.

Output

All the associated values are returned appropriately.

Method 4: Applying the “for” Loop to Find/Get the Last String Occurrence

The “for” loop can also be applied to get the final/last occurrence of a character/substring in an input string.

Example

To find the last instance of the character, use the given code:

string_value = "Welcome to Python Guide"
for i in range(len(string_value) - 1, -1, -1):
    if string_value[i] == 'e':
        print(i)
        break

 

In the above code lines:

  • The string is defined and assigned to the variable named “string_value”.
  • The “for” loop iterates over the indices of the string value in reverse order using the “range()” and “len()” functions.
  • The “if” statement is employed to check/verify if the character at the current index is “e” and if the condition is true, the index is displayed to the console.

Output

Conclusion

To find the last occurrence in the string, various methods such as the “rfind()” method, “rindex()” method, “str.rpartition()” method, or “for” loop are used in Python. This blog demonstrated the approaches to finding the final/last occurrence in a string.

Share Button

Source: linuxhint.com

Leave a Reply