0

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

  • Does this answer your question? [How do I escape backslash and single quote or double quote in Python?](https://stackoverflow.com/questions/6717435/how-do-i-escape-backslash-and-single-quote-or-double-quote-in-python) – Be Chiller Too Nov 15 '21 at 09:51
  • 1
    Welcome to SO! Please don't post pictures. About "the string stored in a database": it is not clear from your question if there are four `"` signs in the data, or only two. If there are four it is a problem because that is not a valid Python string. If there are only two then you do not need to worry about escaping. Escaping quotes is only needed in *string literals*, not when the quotes are in the data. – BoarGules Nov 15 '21 at 09:53
  • Based on the picture, the database mention is spurious; the string is a literal being entered at the `>>>` prompt – Jiří Baum Nov 15 '21 at 10:58

2 Answers2

0

Try:

g = "hello there, user \"X\", please help me solve this issue"
S.B
  • 13,077
  • 10
  • 22
  • 49
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
0

Code -> :

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

Output -> :

 Sammy says, "Hello!"

So answer above is correct. You can use "\" (escape character)

ouflak
  • 2,458
  • 10
  • 44
  • 49
Thyrus
  • 458
  • 2
  • 13