On Jan 9, 11:34 am, cesco <[EMAIL PROTECTED]> wrote:
> Hi,
>
> say I have a string like the following:
> s1 = 'hi_cat_bye_dog'
> and I want to replace the even '_' with ':' and the odd '_' with ','
> so that I get a new string like the following:
> s2 = 'hi:cat,bye:dog'
> Is there a common recipe to accomplish that? I can't come up with any
> solution...
>
> Thanks in advance
> Cesco

For replacing every other one, I tend to use the modulo, with a
counter.

count = (if u want even start with 0 else do 1)

while/for replacing /in lalastring:
  if count % 2 == 0:
    do even
  else:
    do odd
  count += 1

This is a rather basic way of doing it, and I am sure people will sure
much more efficient ways, but it generally works for me.

Though I am now wondering if count % 2 when count == 2 is true or
false as it returns 0

Of course you still need to do your replace but you should be able to
do that.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to