Python does not have automatic formatting. (The .format syntax .format borrowed from C #.) In the end, Perl was "Practical Extraction and Report Language", and Python was not designed to format reports.
Your output can be done using the textwrap module, for example
from textwrap import fill def formatItem(left, right): wrapped = fill(right, width=41, subsequent_indent=' '*15) return ' {0:<13}{1}'.format(left, wrapped) ... >>> print(formatItem('0123', 'This is some long text which would wrap past the 80 column mark and go onto the next line number of times blah blah blah.')) 0123 This is some long text which would wrap past the 80 column mark and go onto the next line number of times blah blah blah.
Note that this assumes that the โleftโ does not span more than one line. A more general solution would be
from textwrap import wrap from itertools import zip_longest def twoColumn(left, right, leftWidth=13, rightWidth=41, indent=2, separation=2): lefts = wrap(left, width=leftWidth) rights = wrap(right, width=rightWidth) results = [] for l, r in zip_longest(lefts, rights, fillvalue=''): results.append('{0:{1}}{2:{5}}{0:{3}}{4}'.format('', indent, l, separation, r, leftWidth)) return "\n".join(results) >>> print(twoColumn("I'm trying to format some strings for output on the command-line", "report style, and am looking for the easiest method to format a string such that I can get automatic paragraph formatting.")) I'm trying to report style, and am looking for the format some easiest method to format a string such strings for that I can get automatic paragraph output on the formatting. command-line
source share