Replace ' with \' in a string

replaced synonym
replace python
replace definition
replace someone
replace movie
replace sentence
how to pronounce replace
words to replace was with

I have a string:

s = r"This is a 'test' string"

I am trying to replace ' with \' so the string will look like below:

s = r"This is a \'test\' string"

I tried s.replace("'","\'") but there is no change in result. It remains the same.

You have to escape the "\":


"\" is an escape sequence indicator, which, to be used as a normal char, has to be escaped (by) itself.

Replace, Replace definition, to assume the former role, position, or function of; substitute for (a person or thing): Electricity has replaced gas in lighting. See more. Replace definition is - to restore to a former place or position. How to use replace in a sentence. Synonym Discussion of replace.

"\'" is still the same as "'" - you have to escape the backslash.

mystr = mystr.replace("'", "\\'")

Making it a raw string r"\'" would also work.

mystr = mystr.replace("'", r"\'")

Also note that you should never use str (or any other builtin name) as a variable name since it will overwrite the builtin, and could cause confusion later on when you try to use the builtin.

>>> mystr = "This is a 'test' string"
>>> print mystr.replace("'", "\\'")
This is a \'test\' string
>>> print mystr.replace("'", r"\'")
This is a \'test\' string

REPLACE, replace verb [T] (PUT BACK). C2. to put something back where it was before: The librarian replaced� To replace is to be or to furnish an equivalent or substitute, especially for one that has been lost, depleted, worn out, or discharged: "We can learn to replace turbulent passions with peaceful emotions" (Margaret Visser).

>>> str = r"This is a 'test' string"
>>> print str
This is a 'test' string
>>> str.replace("'","\\'")
"This is a \\'test\\' string"

You need to escape the special character \

Replace Synonyms, Replace Antonyms, replace � replacere‧place /rɪˈpleɪs/ ○○○ S2 W1 verb [transitive] � 1 REPLACE to start doing something instead of another person, or start being used instead of � Replace definition, to assume the former role, position, or function of; substitute for (a person or thing): Electricity has replaced gas in lighting. See more.

replace, Definition of REPLACE (verb): change something for something new; do someone's job after they leave it; put something back in correct place. replace verb [T] (CHANGE FOR) B1 to take the place of something, or to put something or someone in the place of something or someone else: The factory replaced most of its workers with robots.

REPLACE (verb) definition and synonyms, VerbEdit. replace (third-person singular simple present replaces, present participle replacing, simple past and past participle replaced). (� On the plus side, Replace is one of the most beautifully shot indie horror films I've seen in years, but it almost has to be to keep the audience interested in its moronic script and characters. Even horror icon Barbara Crampton can't save this mess of a movie. 0 of 0 people found this review helpful. Was this review helpful to you? | Report this

replace, Definition and Usage. The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values� Some common synonyms of replace are displace, supersede, and supplant. While all these words mean "to put out of a usual or proper place or into the place of another," replace implies a filling of a place once occupied by something lost, destroyed, or no longer usable or adequate. replaced the broken window

  • What if the input string is r"This is a 'test' string that already contains \'escaped quotes\'"?
  • BTW: You should not use 'str' as a name. It is the name of a built-in.
  • Refer this if you're seeing `\\` in the output…