change a string conditional

Mark McEahern marklists at mceahern.com
Thu May 9 13:31:13 EDT 2002


[obantec support]
> I need to a piece of python code that will take a string which may or may
> not begin with www and change it to lists.

def fixit(s):
  old_prefix = "www"
  new_prefix = "lists"
  if s.startswith(old_prefix):
    s = s.replace(old_prefix, new_prefix, 1)
  return s

l = ["www.whatever.com", "www.whateverwww.com"]
m = []
for x in l:
    m.append(fixit(x))

print m

Cheers,

// mark





More information about the Python-list mailing list