2

I need python to generate exactly such string literal:

e.b=\"e\"

But I can't come up with idea how to do so. I was trying:

r'e.b=\"e\"'      =>     e.b=\\"e\\"
"""e.b=\"e\""""   =>     e.b="e"

And many other possibilities but any ends up with exactly e.b=\"e\"

Any ideas?

santBart
  • 2,466
  • 9
  • 43
  • 66
  • 2
    the first one (`r'e.b=\"e\"'`) should be right. When the python interpreter prints, it will show single `\` characters as `\\`, but if you output it to a file, it should really be just a single `\`. – R Nar May 25 '16 at 19:04
  • Your raw string does what you want. Try printing it with `print`, or writing it to a file. And print its length, which should be 9. – PM 2Ring May 25 '16 at 19:07

2 Answers2

6

Well, you had it right the first time, except that you examined the repr of the string you created, instead of the string itself:

s = r'e.b=\"e\"'
s  # this is the repr() of the string
=> 'e.b=\\"e\\"'
print(repr(s))
=> 'e.b=\\"e\\"'
print(s)  # this is what you want
=> e.b=\"e\"

Bottom line, s=r'e.b=\"e\"' is what you want.

Community
  • 1
  • 1
shx2
  • 61,779
  • 13
  • 130
  • 153
0

Are you thinking of printing like this?

import re
d = re.escape(r'e.b=\"e\"')
print d
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122