Replace all quotes in a string with escaped quotes?

前端 未结 3 1059
遇见更好的自我
遇见更好的自我 2020-12-28 12:36

Given a string in python, such as:

s = \'This sentence has some \"quotes\" in it\\n\'

I want to create a new copy of that string with any q

3条回答
  •  自闭症患者
    2020-12-28 13:01

    Hi usually when working with Javascript I use the json module provided by Python. It will escape the string as well as a bunch of other things as user2357112 has pointed out.

    import json
    string = 'This sentence has some "quotes" in it\n'
    json.dumps(string) #gives you '"This sentence has some \\"quotes\\" in it\\n"'
    

提交回复
热议问题