Search code examples
pythonstringinsertbetween

Insert comma between a specific words in a string in Python


I find lines from a document that contain ' Bon État' with:

if ' Bon État' in i:
   print (i)

res:

0,FENÊTRES Bon État,Présence d’une sérigraphie sur les vitrages,
5,VOLETS / STORES Bon État,,

I want to put a commas just before ' Bon État' like that:

0,FENÊTRES ,Bon État,Présence d’une sérigraphie sur les vitrages,
5,VOLETS / STORES ,Bon État,,

moreover I would like to be able to apply this to something other than ' Bon État'.


Solution

  • The easiest way to do this is to do the following program :

    s = "ok Bon État super"
    s = s.replace("Bon État",",Bon État")