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

jfong at ms4.hinet.net jfong at ms4.hinet.net
Sun Dec 31 05:03:58 EST 2017


Random832於 2017年12月31日星期日 UTC+8下午1時25分50秒寫道:
> 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.

Thank you, it's very helpful. This answer makes me think about '\' more thoughtful:-)

--Jach



More information about the Python-list mailing list