In Python, to find the last occurrence in a string of a character or substring, the easiest way is to use the Python string rfind() function.

string_variable = "This is a string variable we will search."

pos_of_last_a = string_variable.rfind("a")
pos_of_last_w = string_variable.rfind("w")

print(pos_of_last_a)
print(pos_of_last_w)

#Output:
36
29

You can also use the Python string rindex() function, but if the function doesn’t find the character or substring, it will throw an exception.

string_variable = "This is a string variable we will search."

pos_of_last_a = string_variable.rindex("a")
pos_of_last_w = string_variable.rindex("w")

print(pos_of_last_a)
print(pos_of_last_w)

#Output:
36 
29

When working with string variables in Python, being able to search the string variables for substrings and characters is very useful. One such capability is to find the first or last occurrence of a substring in a string.

There are many great string methods in Python, and we can find the last occurrence of a character or substring using the Python string rfind() function.

The rfind() function, or reverse find function, returns the position of the last occurrence of a given string in a string.

If rfind() doesn’t find the given string, then it returns -1.

Below is some sample Python code to find the last occurrence in string of another string.

string_variable = "This is a string variable we will search to try to find some other strings."

pos_of_last_a = string_variable.rfind("a")
pos_of_last_to = string_variable.rfind("to")
pos_of_last_string = string_variable.rfind("string")


print(pos_of_last_a)
print(pos_of_last_to)
print(pos_of_last_string)
print(pos_of_last_z)

#Output:
36
48
67
-1

If you want to find the first occurrence in a string using Python, you can use the find() function.

Finding the Last Occurrence in String Using Start and End Arguments With Python rfind() Function

With the Python rfind() function, we can pass optional “start” and “end” arguments to only search a specific substring of the string for the occurrence of a character or substring.

For example, if we only wanted to search the first 10 characters, we would pass “0” and “10” to rfind().

string_variable = "This is a string variable we will search to try to find some other strings."

pos_of_last_a = string_variable.rfind("a")
pos_of_last_a_first_10 = string_variable.rfind("a",0,10)

print(pos_of_last_a)
print(pos_of_last_a_first_10)

#Output:
36 
8

Finding the Last Occurrence in String of Substring Using Python rindex() Function

Another way we can find the last occurrence of a substring or character in a string is with the Python string rindex() function.

The rindex() function is almost the same as the rfind() function, but if the function doesn’t find the given substring or character, rindex() will throw an exception.

Below are a few examples of using the rindex() function in Python to the last occurrence in string of another string.

string_variable = "This is a string variable we will search."

pos_of_last_a = string_variable.rindex("a")
pos_of_last_w = string_variable.rindex("w")

print(pos_of_last_a)
print(pos_of_last_w)

#Output:
36 
29

Hopefully this article has been useful for you to learn how to find the last occurrence in string of a character or substring using Python.

Categorized in:

Python,

Last Update: March 20, 2024