简体   繁体   中英

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"

I have written an API to fetch the this entry.

Now, I want to escape the double quotes around the letter X to perform some string formatting in python. Is there a way to write a code in python which adds the backslash before the double quotes? 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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