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

  • SEARCH
  • Home
  • 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 8824017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:30:10+00:00 2026-06-14T06:30:10+00:00

The program starts as how many rows? how many coloumns? Alignment of each coloumn?(Left(L),

  • 0

The program starts as how many rows? how many coloumns? Alignment of each coloumn?(Left(L), Centre(C), Right(R)). Then accept entries(data in table) from the user. The entries should be printed in the format specified by the user? Here’s what I have done so far:

rows = input("How many rows?")
coloumns = input("How many coloumns?")
alignment = raw_input("Enter alignment of each table?")
entry = raw_input("Enter rows x cols entries:")
print entry

I think I have to format entry in such a way that it comes out exactly how the user wants. How can I do it? Thanks

  • 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-06-14T06:30:11+00:00Added an answer on June 14, 2026 at 6:30 am

    This block of code referenced from http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/ will help you.

    import locale
    locale.setlocale(locale.LC_NUMERIC, "")
    def format_num(num):
        """Format a number according to given places.
        Adds commas, etc. Will truncate floats into ints!"""
    
        try:
            inum = int(num)
            return locale.format("%.*f", (0, inum), True)
    
        except (ValueError, TypeError):
            return str(num)
    
    
    def get_max_width(table, index):
        """Get the maximum width of the given column index"""
        return max([len(format_num(row[index])) for row in table])
    
    def pprint_table(out, table):
        """Prints out a table of data, padded for alignment
        @param out: Output stream (file-like object)
        @param table: The table to print. A list of lists.
        Each row must have the same number of columns. """
        col_paddings = []
    
        for i in range(len(table[0])):
            col_paddings.append(get_max_width(table, i))
    
        for row in table:
            # left col
            print >> out, row[0].ljust(col_paddings[0] + 1),
            # rest of the cols
            for i in range(1, len(row)):
                col = format_num(row[i]).rjust(col_paddings[i] + 2)
                print >> out, col,
            print >> out
    
    
    table = [["", "taste", "land speed", "life"],
        ["spam", 300101, 4, 1003],
        ["eggs", 105, 13, 42],
        ["lumberjacks", 13, 105, 10]]
    
    import sys
    out = sys.stdout
    pprint_table(out, table)
    

    In your case, because you are collecting inputs of rows, columns, alignment and entry in the table you can plug them in to construct your table variable.

    • len(table[0]) is equivalent to number of columns (-1 to prevent counting in the “y-axis” labels, also known as table index).
    • len(table) is
      equivalent to your number of rows (-1 to prevent counting in the table header).
    • col_padding (alignment) is
      dynamically computed using rjust and ljust methods while calculating a particular column.
    • And each element in your table list can be
      updated using standard python list syntax.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that starts up and creates an in-memory data model and
I want to make a script that starts a program and then sends it
I have a program in which each thread reads in files many lines at
Suppose you have a disease diagnosis Prolog program that starts with many relations between
I have a MDI program. When It starts it takes 2-3MB of RAM. Then,
My program, written in Python 3, has many places where it starts with a
I am using many pthread mutex's. My program uses them and then, at a
When the program starts, a new JFrame is created. Once the user clicks the
I have a program that starts constantly juggles between 3 separate timers. The main
How can I avoid that a user starts the same program twice? The current

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.