-1

Hello I have a string like this

"'Ade', 'Danidee', 'Lamarr', 'O'Connor', 'Austino', 'N'golo',"

I need to be able to escape the single Quote character so the output will be like this

"'Ade', 'Danidee', 'Lamarr', 'O\'Connor', 'Austino', 'N\'golo'," 

I have tried something like

Splitting the string with coma(,) in to a list

loop through it and checking if single quote is in each item

Then

item.replace("'", "\'")

Append it to a new list and join it with (,) again

Is there a way I can do it directly without going through all that?

I need to be able to do something like

str.replace("'", "\'")

But that will replace the whole single quotes

'Ade' will become Ade i don't want that

Thanks.

Sherluck08
  • 53
  • 2
  • 9

2 Answers2

2
import re
text = "'Ade', 'Danidee', 'Lamarr', 'O'Connor', 'Austino', 'N'golo',"
print( re.sub(r"(\w)'(\w)",r"\1\'\2",text) )

'Ade', 'Danidee', 'Lamarr', 'O\'Connor', 'Austino', 'N\'golo',
-1

One way can be:

print("\"'Ade', 'Danidee', 'Lamarr', 'O\\'Connor', 'Austino', 'N\\'golo',\"")