How do I indent a multiline string in the context of string formatting? e.g.
'''
<
%s
>
''' % (paragraph)
where paragraph contains newlines. (‘foo\nbar’)
If I use the above code, I get output like this:
'''
<
foo
bar
>
'''
when I really want this:
'''
<
foo
bar
>
'''
I know I could do something like:
'''
<
%s
>
''' % (paragraph)
but this breaks readability for my purposes.
I also realize I could just write some code to indent all but the first line by 1 indent, but this isn’t really an extensible solution (what if I have 2 indents? or 3? etc.)
EDIT:
Before you post an answer, consider how your solution works with something like this:
'''
<
%s
<
%s
%s
<
%s
>
>
>
''' % (p1, p2, p3, p4)
Okay, the simple doesn’t suit your purposes. Either you need to use a document object model and a flattening system with appropriate indentation.
Or, you could do something fairly hacky to detect the amount of whitespace before each substitution. This is pretty fragile:
Yielding: