alternating string replace

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Jan 9 07:19:52 EST 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")

Bye,
bearophile



More information about the Python-list mailing list