0

I'm trying to escape quote marks in Python, but my intended plan does not work:

>>> s = 'Hi "there"'

>>> s.replace('"', '\"')
'Hi "there"'

>>> s.replace('"', '\\"')
'Hi \\"there\\"'

What is the proper way to single-escape quotes within a string (e.g. make 'Hi \"there\"')?

braaterAfrikaaner
  • 1,072
  • 10
  • 20
duhaime
  • 25,611
  • 17
  • 169
  • 224
  • 2
    You don't need to escape them. – wim Feb 26 '18 at 23:45
  • Yes I do, I'm posting data via TCP sockets and need to escape all sorts of quotes... – duhaime Feb 26 '18 at 23:45
  • 3
    You don't need to escape double quotes to send them over a TCP socket. – wim Feb 26 '18 at 23:47
  • 2
    TCP socket should accept bytes. `'This is my "quoted" string'` is properly escaped already – OneCricketeer Feb 26 '18 at 23:48
  • @wim I have arbitrary strings passing through a pipe that contain all manners of symbols like ` that break posted data. I'd like to escape them all. Question deals with one of these special characters, but I'm looking for a general escape solution... – duhaime Feb 26 '18 at 23:48
  • Escaping them does not change the value, it only makes the syntax correct in special cases – whackamadoodle3000 Feb 26 '18 at 23:49
  • 4
    Then you need a serialiser and/or an encoding, not escaping. – wim Feb 26 '18 at 23:49
  • 2
    If your question is *about sockets*, then post a [mcve] about what you are actually doing. See [XY Problem](http://xyproblem.info/) – OneCricketeer Feb 26 '18 at 23:49
  • 2
    Your second example works perfectly, it's only the output from Python that's confusing you. Letting Python display the raw value doubles up the \\, use `print` to see what's really there. – Mark Ransom Feb 26 '18 at 23:51
  • 2
    @wim You don't know what the poster is trying to achieve. Yes, you can post any data to a socket. This may be a part of a text protocol, or a part of serialising some data. We don't have enough information to say one way or another. Telling the author they don't need it is not a great approach. You can ask for more details if you think that's the case. – viraptor Feb 26 '18 at 23:51
  • 2
    Yeah, well, they didn't post what they are really trying to achieve (classic XY problem). But I know enough about socket communication to say that replacing " with \" is really barking up the wrong tree. – wim Feb 26 '18 at 23:53
  • 1
    @wim cricket_007 ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 viraptor MarkRansom thanks for your comments. I'll delete this and make a new question. Thought I could solve this without getting down to implementation details--looks like I may be wrong... – duhaime Feb 26 '18 at 23:57

1 Answers1

0

Your code is correct. The string that's displayed is the result of repr(your_result) which includes its own escaping. You can get the actual value by printing it.

>>> result = s.replace('"', '\\"')

>>> result
'Hi \\"there\\"'

>>> print(result)
Hi \"there\"

>>> print(repr(result))
'Hi \\"there\\"'
viraptor
  • 33,322
  • 10
  • 107
  • 191
  • I can't delete this question, but didn't know about repr, which helps, so I'll accept this. Never thought a string replacement question would trigger such wild responses! – duhaime Feb 26 '18 at 23:58
  • 1
    The first one is correct. Since the second one has backslashes when it prints, it means it didn't escape the `"`. The first one prints it without backslashes, which means it is escaped. – whackamadoodle3000 Feb 26 '18 at 23:59