How to exec a string which has an embedded '\n'?

Random832 random832 at fastmail.com
Sun Dec 31 00:25:36 EST 2017


On Sat, Dec 30, 2017, at 23:57, jfong at ms4.hinet.net wrote:
> I have a multiline string, something like '''...\nf.write('\n')\n...'''
> when pass to exec(), I got
> SyntaxError: EOL while scanning string literal
> 
> How to get rid of it?

Use \\n for this case, since you want the \n to be interpreted by the exec parser rather than the newline being part of the string.



More information about the Python-list mailing list