You should escape the backslash so that it's not treated as escaping character itself:
Sys.stdout.write("abc\\ndef")
Background
The backslash \
tells the parser that the next character is something special and must be treated differently. That's why \n
will not print as \n
but as a newline. But how do we write a backslash then? We need to escape it, too, resulting in \\
for a single backslash and \\n
for the output \n
.
Docs here, also see this SO question
Alternatively you can use "raw" strings, i.e. prefixing your strings with an r, to disable interpreting escape sequences is your strings:
Sys.stdout.write(r"abc\ndef")