Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 866665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:50:48+00:00 2026-05-15T09:50:48+00:00

I’m trying to format some strings for output on the command-line, report style, and

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:50:48+00:00Added an answer on May 15, 2026 at 9:50 am

    There isn’t automatic formatting like this built into Python. (The .format function 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 textwrap module, e.g.

    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 the “left” does not span over 1 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   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.