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?

2
  • 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 ``. Commented May 25, 2016 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. Commented May 25, 2016 at 19:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you thinking of printing like this?

import re
d = re.escape(r'e.b=\"e\"')
print d

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.