'Convert \"\n string to \\\"\n in Python

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TomasO