The result '\\&' is only displayed - actually the string is \&:
>>> str = '&'
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&
Try it in a shell.
Answer from Emil Ivanov on Stack OverflowThe result '\\&' is only displayed - actually the string is \&:
>>> str = '&'
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&
Try it in a shell.
The extra backslash is not actually added; it's just added by the repr() function to indicate that it's a literal backslash. The Python interpreter uses the repr() function (which calls __repr__() on the object) when the result of an expression needs to be printed:
>>> '\\'
'\\'
>>> print '\\'
\
>>> print '\\'.__repr__()
'\\'
I need to make a string that contains "\" but it doesn't work
SyntaxError: unterminated string literal (detected at line 15)
Quoting backslashes in Python string literals - Stack Overflow
how to insert \ backslash before quotes in string python - Stack Overflow
Variable has a backslash, when the variable is added to create a list, the backslash is duplicated, how can I avoid it?
can't create a string that contains "\"
Videos
You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
Incidentally, there's another string form which might be a bit clearer:
>>> print(r'baz "\"')
baz "\"
Use a raw string:
>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
Note that although it looks wrong, it's actually right. There is only one backslash in the string foo.
This happens because when you just type foo at the prompt, python displays the result of __repr__() on the string. This leads to the following (notice only one backslash and no quotes around the printed string):
>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
And let's keep going because there's more backslash tricks. If you want to have a backslash at the end of the string and use the method above you'll come across a problem:
>>> foo = r'baz \'
File "<stdin>", line 1
foo = r'baz \'
^
SyntaxError: EOL while scanning single-quoted string
Raw strings don't work properly when you do that. You have to use a regular string and escape your backslashes:
>>> foo = 'baz \\'
>>> print(foo)
baz \
However, if you're working with Windows file names, you're in for some pain. What you want to do is use forward slashes and the os.path.normpath() function:
myfile = os.path.normpath('c:/folder/subfolder/file.txt')
open(myfile)
This will save a lot of escaping and hair-tearing. This page was handy when going through this a while ago.
You should add a second \:
your_str= your_str.replace("'", "\\'")
This works, but is not what the OP wanted:
str = "ok I'm ready"
str = str.replace("'", "\\'")
print(str)
EDIT:
Please take a look at this question: A good way to escape quotes in a database query string?
And this other one: Escape string Python for MySQL They used:
conn.escape_string()
EDIT 2: Please see this: Python pymySQL sending a string with quotes
and note how they have used placeholders:
cur.execute('UPDATE connections SET cmd=%s, client_new=1 where ip=%s', (command, ip))