[Python-ideas] Idea for new multi-line triple quote literal

Ron Adam ron3200 at gmail.com
Tue Jul 2 15:27:57 CEST 2013



On 07/02/2013 05:32 AM, Robert Kern wrote:
>> s = """\
>>      first line
>>      second line ... """
>>
>> But in any case, I don't like the idea of making the proposed dedent
>> method be a DWIM "format strings the way I want them to be formatted"
>> method. It's called "dedent", not "dedent and strip leading newlines".
>>
>> I'm okay in principle with dedent taking additional arguments to
>> customize the behaviour, such as amount of whitespace to keep or a
>> margin character, so long as the default with no args matches
>> textwrap.dedent.
>
> How about an option to ignore the first line?

A method that only adjusts leading space should only do that.


I wrote a function to split a text string into paragraphs, and I found that 
it made sense for that to strip leading space and trailing white space.

The reason is, it is not clear if the leading or trailing white space is an 
empty paragraph or not.  If you treat those as empty paragraphs at the 
beginning and end, then you end up with too much white space when you join 
them back together after re-flowing the paragraphs.




It seems the wrap and fill functions in textwrap have some issues too. 
There is an issue on the bug tracker, but the exact nature of the problems 
and weather or not anyone is depending on the current behavior.

The textwrap module, doesn't remove extra white space inside the string, 
but only does a strip, removing leading and trailing white space.  So 
leading white space on lines gets folded into the reflowed text.  That 
can't be what was originally intended.

from textwrap import *
s = """

      This is a
      multi-line string that
      has     no
                     meaningful
      formatting, to see what fill
            and wrap do to
      it.

      """

print('"""' + fill(s) + '"""')

"""       This is a       multi-line string that      has     no
meaningful      formatting, to see what fill            and wrap do to
it."""
print

for x in wrap(s, 40):
     print('"""'+x+'"""')

"""       This is a       multi-line string that      has     no
meaningful      formatting, to see what fill            and wrap do to
it."""
"""       This is a       multi-line string"""
"""that      has     no"""
"""meaningful      formatting, to see what"""
"""fill            and wrap do to      it."""


Cheers,
    Ron








More information about the Python-ideas mailing list