Reputation: 60
I am creating a program that contains all my other programs however when I attempt to write to the file it prints \n
as a new line rather than literally \n
.
For Example:
file.write("""a=input("What would you like?\n")
Produces:
a=input("What would you like?
")
Is there any way around this?
Upvotes: 2
Views: 1659
Reputation: 13721
Use the backslash \
to escape characters that otherwise have a special meaning such as the
newline, backslash itself, or the quote character for example.
Therefore use:
file.write("""a=input("What would you like?\\n")""")
Or make it a raw string by adding a r
in front of the string:
file.write(r"""a=input("What would you like?\n")""")
Upvotes: 3