Seeking assistance - string processing.

Fredrik Lundh fredrik at
Tue Nov 14 11:42:36 CET 2006

billpaterson2006 at wrote:

> I am trying to work out how to make it find a string like this "==="
> and when it has found it, I want it to add "===" to the end of the
> line.

how about

     if line.startswith("==="):
         line = line + "==="


     if "===" in line: # anywhere
         line = line + "==="


>         if line.rfind("--------------------") is not -1:
>             new = line.replace("--------------------","----")

it's not an error to use replace on a string that doesn't contain the 
pattern, so that rfind is rather unnecessary.

(and for cases where you need to look first, searching from the left
is usually faster than searching backwards; use "pattern in line" or 
"line.find(pattern)" instead of rfind.


More information about the Python-list mailing list