How about:

len(a) - a[-1::-1].index("hello") - 1

Edit (put in function as suggested):

def listRightIndex(alist, value):
    return len(alist) - alist[-1::-1].index(value) -1
Answer from EwyynTomato on Stack Overflow
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ rindex
Python String rindex()
... If substring exists inside ... is similar to rfind() method for strings. The only difference is that rfind() returns -1 if the substring is not found, whereas rindex() throws an exception....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_rindex.asp
Python String rindex() Method
The rindex() method finds the last occurrence of the specified value. The rindex() method raises an exception if the value is not found. The rindex() method is almost the same as the rfind() method.
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ references โ€บ methods-and-functions โ€บ methods โ€บ string โ€บ rindex โ€บ python-string-index
Python rindex() function | Why do we use Python String rindex() function? |
September 27, 2021 - Both rindex() and rfind() are identical ... The main difference is that Python rfind() produces -1 as output if it is unable to find the substring, whereas rindex() throws a ValueError exception....
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2343176 โ€บ what-is-the-difference-between-rfind-index
What is the difference between rfind() & index()?
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ getting the index for strings: str.index(), str.rindex() and str.find(), str.rfind()
Python Language Tutorial => Getting the index for strings:...
Similarities in syntax, Differences in meaning: Python vs. JavaScript ... String also have an index method but also more advanced options and the additional str.find. For both of these there is a complementary reversed method. astring = 'Hello on StackOverflow' astring.index('o') # 4 astring.rindex('o') # 20 astring.find('o') # 4 astring.rfind('o') # 20 ยท The difference between index/rindex and find/rfind is what happens if the substring is not found in the string:
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_rindex.htm
Python String rindex() Method
Python string method rindex() searches for the last index where the given substring str is found in an original string. This method raises an exception if no such index exists, optionally restricting the search to string length, i.e. from the ...
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-string-rindex-method
Python String rindex() Method - Learn By Example
April 20, 2020 - The rindex() method searches for the last occurrence of the specified substring sub and returns its index.
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .rindex()
Python | Strings | .rindex() | Codecademy
October 6, 2023 - .rindex() is used to locate the highest index of the substring within a string variable. A ValueError will be raised when the substring cannot be found. ... Looking for an introduction to the theory behind programming?
Top answer
1 of 2
10

Is there any significant difference in term of speed other than potential overheads if we have to enclose s.index() within a try/except.

In (C)Python at least, find, index, rfind, rindex are all wrappers around an internal function any_find_slice.

The implementation is the same. The only difference is that index and rindex will raise a ValueError for you if it finds that the result of calling any_find_slice is -1.

If you went ahead and timed these you'd see how there's clearly no meaningful difference between them:

โžœ  ~ python -m perf timeit -s "s = 'a' * 1000 + 'b'" "s.find('b')"
Median +- std dev: 399 ns +- 7 ns
โžœ  ~ python -m perf timeit -s "s = 'a' * 1000 + 'b'" "s.index('b')"
Median +- std dev: 396 ns +- 3 ns

I'm using perf for the timings here.

I'm guessing in other implementations of Python this shouldn't differ. Both methods do the same thing and differ only in how they react when the requested element was not found.

2 of 2
1

@Dimitris's answer showed that s.find() and s.index() perform equally well if the substring is found.

But as @augustomen pointed out, if the substring is not found then s.index() will be significantly slower due to the exception handling. We can test this with the following code snippets.

Note that I'm using a different machine to @Dimitris so my timings cannot be compared with his.

$ python3 -m timeit -s "s = 'a' * 1000" "s.find('b')"
5000000 loops, best of 5: 87.3 nsec per loop
$ python3 -m timeit -s "s = 'a' * 1000" "try: s.index('b')" "except ValueError: pass"
1000000 loops, best of 5: 242 nsec per loop

It's clear that s.find() is the winner when the substring is not found, but is that just because we didn't include a try/except block for it?

Let's try adding a try/except block to s.find() (even though we know it won't be triggered).

$ python3 -m timeit -s "s = 'a' * 1000" "try: s.find('b')" "except ValueError: pass"
5000000 loops, best of 5: 89.1 nsec per loop

We see here that the mere presence of a try/except block barely alters the time at all. It's only when the exception is actually triggered that we incur a meaningful hit to performance.

The moral of the story is to use s.find() if there's a reasonable chance that the substring won't be found.

If you're sure that the substring will almost always be found then you can use either s.find() or s.index(). You might prefer s.index() in that case, because the try/except syntax signals to other developers that you are handling an edge case that you don't expect will occur very often.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-string-rindex-method
Python String rindex() Method - GeeksforGeeks
January 7, 2025 - Python String rindex() method returns the highest index of the substring inside the string if the substring is found. Otherwise, it raises ValueError. ... Note: If start and end indexes are not provided then by default Python String rindex() ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pandas-series-str-rindex
Python | Pandas Series.str.rindex() - GeeksforGeeks
July 11, 2025 - Output: As shown in the output image, It can be compared that .index() method returned the least index while the str.rindex() method returned highest index. Example #2: In this example, 'a' is searched in top 5 rows.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ string-rindex-python
String rindex() in python - GeeksforGeeks
January 9, 2018 - Get access to ad-free content, doubt assistance and more! ... The rindex() method returns the highest index of the substring inside the string if substring is found.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-string-rindex-method
Python String | rindex() method with Examples - Javatpoint
We are excited to announce that we are moving from JavaTpoint.com to TpointTech.com on 10th Feb 2025. Stay tuned for an enhanced experience with the same great content and even more features. Thank you for your continued support ยท Python rindex() method works same as the rfind() method except ...
๐ŸŒ
DEV Community
dev.to โ€บ hyperkai โ€บ index-rindex-in-python-51j9
index & rindex in Python - DEV Community
August 3, 2025 - The 1st argument is sub(Required-Type:str for str.rindex() or bytes-like object for bytes.rindex() and bytearray.rindex()): It's the substring or byte substring of zero or more characters. Don't use sub=. The 2nd argument is start(Optional-Type:int or NoneType): It's a start index.
๐ŸŒ
YouTube
youtube.com โ€บ techno stack
#9 Python String Methods : index , rindex - YouTube
Welcome geeks!welcome to the series on python programming. This series will cover A to Z of python programming concepts and coding practices. stay tuned to w...
Published: March 29, 2021
Views: 2
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ str โ€บ rinddex.html
rindex โ€” Python Reference (The Right Way) 0.1 documentation
Returns the index of the first occurrence of the string searched for (raises ValueError if not found). ... Required. The string searched for. ... Optional. Search start position. ... Optional. Search end position. ... >>> "ABAB".rindex("B") 3 >>> "ABAB".rindex("B", 2, 4) 3 >>> "ABAB".rindex("B", ...
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ python โ€บ reference โ€บ methods โ€บ string โ€บ python-string-rindex-method
Python String rindex() Function | Tutorial Reference
The String rindex() method searches for the last occurrence of the given string and returns its index. If specified string is not found, it raises a ValueError exception. ... If you want to get the first occurrence of the specified string, then you have to use the index() method.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-rindex
Python rindex
April 1, 2025 - Python rindex method return the index position of the last occurrence of a specified string. If not found, Python rindex returns Value Error.