[Python-ideas] Multi-line strings that respect indentation

Guido van Rossum guido at python.org
Fri Nov 5 01:35:45 CET 2010


IIRC Scala has triple-quoted strings with an added feature for this.
We should ask some Scala users if they like it.

On Thu, Nov 4, 2010 at 5:21 PM, Daniel da Silva <ddasilva at umd.edu> wrote:
>
> On several occasions I have run into code that will do something like the
> following with a multiline string:
>
>
>> def some_func():
>>     x, y = process_something()
>>
>>     val = """
>> <xml>
>>   <myThing>
>>     <val>%s</val>
>>     <otherVal>%s</otherVal>
>>   </myThing>
>>
>> </xml>
>> """ % (x, y)
>>
>>     return val
>
> To me, this is rather ugly because it messes up the indentation of
> some_func(). Suppose we could have a multiline string, that when started on
> a line indented four spaces, ignores the first four spaces on each line of
> the literal when creating the actual string?
>
> In this example, I will use four quotes to start such a string. I think the
> syntax for this could vary though. It would be something like this:
>
>> def some_func():
>>     x, y = process_something()
>>
>>     val = """"
>>     <xml>
>>       <myThing>
>>         <val>%s</val>
>>         <otherVal>%s</otherVal>
>>       </myThing>
>>     </xml>
>>     """" % (x, y)
>>
>>     return val
>
> That way, the indentation in the function would be preserved, making
> everything easy to scan, and the indentation in the output would not suffer.
>
>
> What do you all think?
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>



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



More information about the Python-ideas mailing list