alternating string replace

cokofreedom at gmail.com cokofreedom at gmail.com
Wed Jan 9 08:08:03 EST 2008


Designed a pretty basic way that is "acceptable" on small strings.

evenOrOdd = True
s1 = "hi_cat_bye_dog_foo_bar_red"
s2 = ""

for i in s1:
    if i == '_':
        if evenOrOdd:
            s2 += ':'
            evenOrOdd = not evenOrOdd
        else:
            s2 +=  ','
            evenOrOdd = not evenOrOdd
    else:
        s2 += i

print s2



More information about the Python-list mailing list