Indented multi-line strings

MRAB python at mrabarnett.plus.com
Wed May 23 16:45:06 EDT 2018


On 2018-05-23 19:36, Mikhail V wrote:
> On Wed, May 23, 2018 at 8:08 PM, Mikhail V <mikhailwas at gmail.com> wrote:
>> On Wed, May 23, 2018 at 4:19 PM, Dan Strohl <D.Strohl at f5.com> wrote:
> 
>> data = /// sN         # and
>> data = /// tN
>>
>> Where N - is the amount of characters, spaces (s) or
>> tabs (t).
>> This should cover most use cases.
>> It implies of course that the user should know himself
>> what he is doing.
>>
>> More concrete example:
>>
>> def func():
>>     foobar
>>     data = /// s2
>>       first line
>>       last line
>>     foobar
>>
>> will store same data as:
>> data = "first linelast line"
> 
> oops, I meant it to be:
> data = "first line\nlast line"
> 
> sorry for possible confusion
> 
>>
>> (assuming of course no trailing spaces were
>> in original lines)
> 
Instead of the "s2", etc:

def func():
     foobar
     data = >> :
       first line
       last line
     foobar

Leading indentation to the level of the first line would be stripped.

As the last line also ends with '\n', the result should be 'first 
line\nlast line\n'.

If you want additional indentation, then provide a string literal:

def func():
     foobar
     data = >> '    ':
       first line
       last line
     foobar

for '    first line\n    last line\n' or:

def func():
     foobar
     data = >> '\t':
       first line
       last line
     foobar

for '\tfirst line\n\tlast line\n'.



More information about the Python-list mailing list