오은아은아 오은아은아 - 4 months ago 9
Python Question

I want adding string @ in every second comma

a = 'a,b,c,d,e,f,g,e'


I want to change
'a,b,c,d,e,f,g,e'
to
'a,b@c,d@e,f@g,e'
.

Is it possible?

Answer

For the regex lovers:

import re

input = 'a,b,c,d,e,f,g,e'

output = re.sub(r',([^,]*),', r',\1@', input)