0

I'm trying to replace this: \ in a specific string:

'"Noir c\'est noir", ont-ils dit, y a donc vraiment plus d\'espoir'

But when I use .replace('\\',''), the result is :

'"Noir c\'est noir", ont-ils dit, y a donc vraiment plus d\'espoir'
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Quxntin
  • 39
  • 5
  • you cant use double and single quotes together, use double for the string – flaxon Apr 26 '22 at 19:07
  • Is there *actually* a slash in the string, or is it a consequence of how you're printing it? If you do `print(your_string)` does it still show a slash? – Silvio Mayolo Apr 26 '22 at 19:08
  • So what is wrong with the output you get? – AlirezaAsadi Apr 26 '22 at 19:08
  • Yes I see the slash when I print it – Quxntin Apr 26 '22 at 19:09
  • There is no slash!? – Ulrich Eckhardt Apr 26 '22 at 19:10
  • 1
    Please include the code that defines the string, and/or the result of printing it (along with the code that prints it). If I copy and paste your string as-is into a Python interpreter, it does not contain actual backslash characters. – Samwise Apr 26 '22 at 19:10
  • Anyhow, how precisely do you print it? Using `repr()` or via the interactive commandline? In that case, escaping quotes is expected when they're in the middle of a string. – Ulrich Eckhardt Apr 26 '22 at 19:10
  • I don't want the \ in my result – Quxntin Apr 26 '22 at 19:11
  • 4
    I think it is very likely that (as a few others have pointed out) **there is no backslash in your actual string**. You're just seeing backslashes because you're printing its `repr`, which adds \ characters to escape the `'` inside the string. Note that if you print a list/dict/etc that contains a string, it will show the `repr` of the string in order to disambiguate it from the rest of the formatting. – Samwise Apr 26 '22 at 19:12
  • I edited your post to improve the formatting (i.e. show code as code), but it required changing the number of backslashes, but I'm sure I got it correct since otherwise the string syntax would be invalid. – wjandrea Apr 26 '22 at 19:12
  • @flaxon but the string contains double quotes... – wjandrea Apr 26 '22 at 19:13
  • 1
    @Quxntin: Please provide a [mcve]. As a new user here, please also take the [tour] and read [ask]! – Ulrich Eckhardt Apr 26 '22 at 19:14
  • @flaxon A single-quoted string can contain literal double quotes, and vice versa. `'"This is fine."'` `"'So is this.'"` – jjramsey Apr 26 '22 at 19:18
  • If I run `print('"Noir c\'est noir", ont-ils dit, y a donc vraiment plus d\'espoir')`, the output is `"Noir c'est noir", ont-ils dit, y a donc vraiment plus d'espoir`. – jjramsey Apr 26 '22 at 19:20
  • @jjramsey if you want to have both in the string then the one surrounding the string need to be escaped – flaxon Apr 27 '22 at 14:18

1 Answers1

3

There is no backslash in the string. The backslash that you see in the representation of the string is an escape character to indicate that the single quote is literal, and doesn't mark the end of the string. If you print the string, you'll see that.

s = '"Noir c\'est noir", ont-ils dit, y a donc vraiment plus d\'espoir'
print(s)

Output:

"Noir c'est noir", ont-ils dit, y a donc vraiment plus d'espoir

To further illustrate, another way to create the same string is with triple-quotes and no backslashes:

s = '''"Noir c'est noir", ont-ils dit, y a donc vraiment plus d'espoir'''
print(s)

And we get the same output:

"Noir c'est noir", ont-ils dit, y a donc vraiment plus d'espoir

And if we print(repr(s)), we'll get the same representation as the original:

'"Noir c\'est noir", ont-ils dit, y a donc vraiment plus d\'espoir'

Related questions:

wjandrea
  • 28,235
  • 9
  • 60
  • 81