alternating string replace

Paddy paddy3118 at googlemail.com
Fri Jan 11 11:00:28 EST 2008


On Jan 11, 9:31 am, cokofree... at gmail.com wrote:
> evenOrOdd = True
> s1, s2 = "hi_cat_bye_dog_foo_bar_red", ""
>
> for i in s1:
>    if i == '_':
>        s2 += ':' if evenOrOdd else ','
>        evenOrOdd = not evenOrOdd
>    else:
>        s2 += i
>
> print s2
>
> Presently I cannot work out how to use .join instead of += ...
Do
s2 = []
then later: s2.append(i)

and at the end: print ''.join(s2)


- Paddy.

> While I realise this is producing a new string (and I believe +=
> rebuilds it a lot?) how much slower
> is this going to be over the others?




More information about the Python-list mailing list