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'
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'
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: