3

I have a string that contains quotes like this:

string = "\"This is my mom's vase\", said Kevin."

Problem is when I use it as a string in python it adds a backslash before single quotes, as seen here:

>>> string
'"This is my mom\'s vase", said Kevin.'
>>>

Why is this happening and what can I do about it?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
agnel
  • 631
  • 1
  • 7
  • 9

3 Answers3

5

The Explanation

What you're seeing is the representation of your string as produced by the repr function. repr outputs strings in such a way that they're valid python literals; in other words, you can copy/paste the output of repr(string) into a python program without getting a syntax error:

>>> string
'"This is my mom\'s vase", said Kevin.'
>>> '"This is my mom\'s vase", said Kevin.'  # it's valid python code!
'"This is my mom\'s vase", said Kevin.'

Because your string contains both single quotes ' and double quotes ", python has to escape one of those quotes in order to produce a valid string literal. The same way you escaped your double quotes with backslashes:

"\"This is my mom's vase\", said Kevin."

Python instead chooses to escape the single quotes:

'"This is my mom\'s vase", said Kevin.'

Of course, both of these strings are completely identical. Those backslashes are only there for escaping purposes, they don't actually exist in the string. You can confirm this by printing the string, which outputs the string's real value:

>>> print(string)
"This is my mom's vase", said Kevin.

The Solution

There's nothing to solve! What are you still doing here? Scroll up and read the explanation again!

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • BTW if a string contains single-quotes but no double-quotes, the repr will be inside double-quotes, for readability. E.g. `'mom\'s'` -> `"mom's"` – wjandrea Jun 09 '20 at 23:45
2

It's just escaped in the REPL. If you print it out, it will show there is no slash added:

print(string)
# output: "This is my mom's vase", said Kevin.
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • And "the repl" just means "the command-line where you try things out". http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop – steveha Jun 05 '14 at 06:04
  • And they are escaped because in the command line strings are enclosed with single quotes – Tim Jun 05 '14 at 06:05
-1

I found the solution, it has nothing to do with repr(string), as @Aran-Fey mentions, it has to do with API and Jsons response.

The correct approach is that you are not returning strings or jsons dumps, but response which http protocols interprets as you mention: \" (backslaches every string).

The solution is to make a http response as follows:

from flask import request, jsonify, make_response
from flask_restful import Resource
from flask_api import status

class employeeHiring(Resource):
    def post(self):
    #YOUR CODE ....
    return make_response(jsonify({'status': 'success', 'my Dict': dict}), status.HTTP_201_CREATED)
  • I think you're answering a different question – wjandrea Jun 09 '20 at 23:38
  • Oh, you're answering your own question that you [posted as an answer](https://stackoverflow.com/a/56861443/4518341). Please don't post questions as answers. Instead, you can ask a new question and [answer it yourself](/help/self-answer). – wjandrea Jun 09 '20 at 23:39