-1

I have a string \"\n in python and I would like to convert it to \\\"\n. How can I do that in general? I need that because I would like to pass this string as an argument to function written in C# since in C# \\\"\n string is simply translated to \"\n

jujus
  • 149
  • 7
  • If you know what result you expect `(\\\\"\n)` why don't you just return it? – kosciej16 Apr 22 '22 at 21:59
  • 2
    You might mix up the representation of a string with the real content of the string. Look at the difference of `print(r'\"\n')` and `print(repr(r'\"\n'))`, – Matthias Apr 22 '22 at 22:00

1 Answers1

0

I found this link Convert regular Python string to raw string

s = '\"\n'
raw_s = s.encode('unicode-escape').decode().replace('\"', '\\\"')
final_s = raw_s.replace(r'\"\n',r'\\\"\n')

print(s)
print(raw_s)
print(final_s)

Output:

"

\"\n
\\\"\n
TomasO
  • 173
  • 2
  • 7