Python

Python Find Last Occurrence in String

In python, if the string contains a replication of the same number, it is known as the occurrence of a Python string. Certainly, when we are working with python strings, we need to find the last occurrence in the string. The issue is very common, and we are here to fix this issue. The way of getting the last existence of the string is illustrated here. Let’s discuss a few methods in which we performed the operations to fetch the last occurrence of the Python string. So, head over to Spyder Compiler and dry run all the example codes to understand all methods briefly.

Example 1:

Here we illustrate the most reliable way to get the last occurrences of the string items by using the string rindex() method. The rindex() command returns the last existence of the substring or character if present in the Python string. The con of this approach is that it generates an error if there is no character or substring in the Python string and hence disrupts the program code.

To further illustrate the rindex() function, we use the Spyder compiler so that you can understand how it searches the occurrence of the last character or substring in the python string. Initially, we create and initialize a python string. Then we use a print function that displays the created Python string. We can then call the rindex() function and pass our string as a parameter. The print function is utilized to show the index of the last existence of the python string.

first_string = "Python is best for learning"

target_string = "for"

print("My first string : " + str(first_string))

result = first_string.rindex(target_string)

print("last occurrence of substring index is : " + str(result))


To find the last occurrence of the string, save and execute the program, and the output is displayed on the compiler console screen.

Example 2:

In our second demonstration, another most efficient method is to find the last occurrences of the string by using the string rfind() method. This is another method to attain the goal. This command is much better than the aforementioned way as it returns a “-1” if it doesn’t find any substring. It doesn’t generate an error, just like the rindex() method.

To further illustrate the rfind() function, we use the same program file so that you can understand how it searches the occurrence of the last character or substring in the python string. Initially, we create and initialize a python string ‘Python is the best programming language’. Then we use a print function that displays the created Python string. We can then call the rfind() function and pass our string as a parameter. We can then use the print function to display the index of the last occurrence of a substring in the python string.

first_string = "Python is the best programming language"

target_string = " programming "

print("My first string : " + str(first_string)

result = first_string.rfind(target_string)

print("last occurrence of substring index is : " + str(result))


To find the last occurrence of the string, save and execute the program, and the output is displayed on the compiler console screen.

Example 3:

Our third program takes input from the user using the rindex() function. However, we use the rindex() method to find the last character index in a Python string in this program. It searches the last index and then returns the value. So, here we are creating the string which takes input from the user and saves that value in “first_str”. Similarly, we are reading the string char and assigning that element in “sec_char”. The existence of “sec_char” in “first_str” is found using the rindex command, and it is saved in “last_index” and then the print function displays that index value.

first_str = input("Enter a string : ")

sec_char = input("Enter a character : ")

last_index = first_str.rindex(sec_char)

print(last_index)


Here we dry run our program code two times. The first time it generates a “ValueError” because it can find “z” in the given string. The second time it finds the value and prints out the resultant value on the screen.

To find the last occurrence of the string, save and execute the program, and the output is displayed on the compiler console screen.

Conclusion:

These were the basic programs to find the last occurrence in Python string. Just try these methods and understand the concept of rindex() and rfind() methods. It is suggested to implement all these methods on your systems as well.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content