Replacing a text with \n in it, with a real \n output

In python 3+, the best way to interpret all escape characters is:

print(f"{yourStringHere}")

This uses f-strings which, in my opinion, is probably the most elegant way to solve this issue.


If you're running this in the Python interpreter, it is the regular behavior of the interpreter to show newlines as "\n" instead of actual newlines, because it makes it easier to debug the output. If you want to get actual newlines within the interpreter, you should print the string you get.

If this is what the program is outputting (i.e.: You're getting newline escape sequences from the external program), you should use the following:

OUTPUT = stdout.read()
formatted_output = OUTPUT.replace('\\n', '\n').replace('\\t', '\t')
print formatted_output

This will replace escaped newlines by actual newlines in the output string.