简体   繁体   English

如何在python中的双引号字符串中转义双引号?

[英]How to escape a double quote inside double quoted string in python?

I have a string stored in a database- "hello there, user "X", please help me solve this issue"我在数据库中存储了一个字符串-“你好,用户“X”,请帮我解决这个问题”

I have written an API to fetch the this entry.我已经编写了一个 API 来获取这个条目。

Now, I want to escape the double quotes around the letter X to perform some string formatting in python.现在,我想转义字母 X 周围的双引号以在 python 中执行一些字符串格式化。 Is there a way to write a code in python which adds the backslash before the double quotes?有没有办法在 python 中编写代码,在双引号之前添加反斜杠? I have tried using replace function, it will give invalid syntax.我试过使用替换功能,它会给出无效的语法。 I just cannot use any function to escape it as it gives invalid syntax anyway.我只是不能使用任何函数来转义它,因为它无论如何都会提供无效的语法。 Check the image attached for reference.检查附加的图像以供参考。

Error image错误图片

Code -> :代码 -> :

 print("Sammy says, \"Hello!\"")

Output -> :输出 -> :

 Sammy says, "Hello!"

So answer above is correct.所以上面的答案是正确的。 You can use "\\" (escape character)您可以使用“\\”(转义字符)

试试: g = "hello there, user \\"X\\", please help me solve this issue"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM