Paul Rudin wrote: > Peter Otten <__pete...@web.de> writes: > > >> OP: you may be looking for >> >>>>> a = "a bb ccc" >>>>> a[::-1].find(" ") >> 3 > > > But you should be aware of the effeciency implications of doing > this. a[::-1] constructs a new list.
A new string, yes. > It's probably faster to do e.g.: > len(a) - a.rfind(..) - 1 Yes, especially if the string is "long": $ python -m timeit -s'a = "word1 word2 word3"' 'a[::-1].rfind(" ")' 1000000 loops, best of 3: 0.834 usec per loop $ python -m timeit -s'a = "word1 word2 word3"*100' 'a[::-1].rfind(" ")' 100000 loops, best of 3: 5.04 usec per loop $ python -m timeit -s'a = "word1 word2 word3"' 'len(a)-a.rfind(" ")-1' 1000000 loops, best of 3: 0.587 usec per loop $ python -m timeit -s'a = "word1 word2 word3"*100' 'len(a)-a.rfind(" ")-1' 1000000 loops, best of 3: 0.592 usec per loop But be aware of the following difference: >>> a[::-1].rfind("not there") -1 >>> len(a) - a.rfind("not there") -1 8 Peter -- http://mail.python.org/mailman/listinfo/python-list