alternating string replace

bearophileHUGS at bearophileHUGS at
Wed Jan 9 13:19:52 CET 2008

My version, uses a re.sub, plus a function used as an object with a
one bit state:

from re import sub

def repl(o):
    repl.n = not repl.n
    return ":" if repl.n else ","
repl.n = False

print sub("_", repl, "hi_cat_bye_dog_foo_bar_red")


More information about the Python-list mailing list