1
list1 = 'it is, a good,day'

I need to separate all the parts of the string by the comma : 'it is', 'a good' and 'day' and then place 'ouf' in between each part so it could print it is ouf a good ouf day

Note the space after the coma at ', a good' is there also a way to delete the space only after the comma?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Bob Ebert
  • 1,342
  • 4
  • 22
  • 41

1 Answers1

4

You can use split, strip, join and map

>>> list1 = 'it is, a good,day'
>>> ' ouf '.join(map(str.strip,list1.split(',')))
'it is ouf a good ouf day'

Alternatively you can try replace

>>> list1.replace(', ',',').replace(',',' ouf ')
'it is ouf a good ouf day'

Considering the edit, I would suggest re module's sub function.

>>> re.sub(r',\s?',' ouf ',list1)
'it is ouf a good ouf day'
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140