string.find first before location

Peter Otten __peter__ at web.de
Tue May 2 09:15:36 CEST 2006


Gary Wessle wrote:

> I have a string like this
> 
> text = "abc abc and Here and there"
> I want to grab the first "abc" before "Here"
> 
> import string
> string.find(text, "Here") # <type int>
> 
> I am having a problem with the next step.

These days str methods are preferred over the string module's functions.

>>> text = "abc abc and Here and there"
>>> here_pos = text.find("Here")
>>> text.rfind("abc", 0, here_pos)
4

Peter



More information about the Python-list mailing list