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.
In perlform formatting is done through the “format” function
format Something =
Test: @<<<<<<<< @||||| @>>>>>
$str, $%, '$' . int($num)
.
$str = "widget";
$num = $cost/$quantity;
$~ = 'Something';
write;
Variations of the perlform allow texts to be wrapped cleanly, useful for help screens, log reports and such.
Is there a python equivalent? Or a reasonable hack that I could write using Python’s new string format function?
Example output I’d like:
Foobar-Title Blob
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.
hi there dito
something more text here. more text here. more text
here.
There isn’t automatic formatting like this built into Python. (The
.formatfunction syntax is borrowed from C#.) After all, Perl was “Practical Extraction and Report Language” and Python isn’t designed for formatting reports.Your output could be done with the
textwrapmodule, e.g.Note that this assumes the “left” does not span over 1 line. A more general solution would be