[Python-Dev] eval and triple quoted strings

Guido van Rossum guido at python.org
Tue Jun 18 00:35:12 CEST 2013

On Mon, Jun 17, 2013 at 3:18 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Guido van Rossum wrote:
>> No. Executing a file containing those exact characters produces a
>> string containing only '\n' and exec/eval is meant to behave the same
>> way. The string may not have originated from a file, so the universal
>> newlines behavior of the io module is irrelevant here -- the parser
>> must implement its own equivalent processing, and it does.

> I'm still not convinced that this is necessary or desirable
> behaviour. I can understand the parser doing this as a
> workaround before we had universal newlines, but now that
> we do, I'd expect any Python string to already have newlines
> converted to their canonical representation, and that any CRs
> it contains are meant to be there. The parser shouldn't need
> to do newline translation a second time.

There are other ways to get a string besides reading it from a file.

Anyway, I think that if you want a string literal that contains \r\n
as its line endings, you should use a syntactic solution, and the
syntax ought to be the same regardless of whether you are reading it
from a file or from a string literal. That syntactic solution is very

"""line one\r
line two\r
line three\r

This works everywhere.

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list