Triple quoted repr

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed Jun 2 23:25:35 EDT 2004


Edward C. Jones wrote:

> On 2003-09-04, Rasmus Fogh said:
> 
>  > I need a way of writing strings or arbitrary Python code that will
>  >
>  > a) allow the strings to be read again unchanged (like repr)
>  > b) write multiline strings as multiline strings instead of escaping
>  > the \n's.
>  >
>  > A repr function that output triple-quoted strings with explicit
>  > (non-escaped) linebreaks would be perfect.
>  >
>  > Failing that, what is the simplest way to get close to the goal?
> 
> There were no responses. Anyone have an answer?

Perhaps something like:

s = repr("'''multi\n'line'\nstring'''")
s = "'''%s'''" % (s[1:-1].replace('\\n', '\n').replace("'''",
"\\'\\'\\'"),)

which changes \n to a linefeed, \r to a carriage return, and ''' to an
escaped form (in case you have a triple-quoted string with the same
quote character).

Tim Delaney




More information about the Python-list mailing list