-1

I have some data(List data,particularly) the look like this when I print out

 PROBABILITY AND STATISTICS B+
 DISCRETE MATHEMATICS A
 DATABASE MANAGEMENT SYSTEMS C+
 FORMAL LANGUAGES AND AUTOMATA THEORY A
 INTERNET TECHNOLOGIES A
 MANAGERIAL ECONOMICS AND FINANCIAL ANALYSIS A
 DATABASE MANAGEMENT SYSTEMS LAB A++
 INTERNET TECHNOLOGIES LAB A++
 COMPREHENSIVE ONLINE EXAMINATION B

but I want like this:

 PROBABILITY AND STATISTICS                  B+
 DISCRETE MATHEMATICS                        A
 DATABASE MANAGEMENT SYSTEMS                 C+
 FORMAL LANGUAGES AND AUTOMATA THEORY        A
 INTERNET TECHNOLOGIES                       A
 MANAGERIAL ECONOMICS AND FINANCIAL ANALYSIS A
 DATABASE MANAGEMENT SYSTEMS LAB             A++
 INTERNET TECHNOLOGIES LAB                   A++
 COMPREHENSIVE ONLINE EXAMINATION            B

How can I achieve this in python?

Harry
  • 33
  • 1
  • 9
  • 2
    See [here](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) or [here](https://stackoverflow.com/questions/9989334/create-nice-column-output-in-python) or search for "pretty print table" as this is a fairly common topic. – Cory Kramer Oct 12 '17 at 13:14
  • Looks like you want `ljust`. See [here](https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces) – Nick is tired Oct 12 '17 at 13:21
  • [Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.](http://meta.stackoverflow.com/questions/254393) –  Jan 08 '18 at 07:05
  • Possible duplicate of [Printing Lists as Tabular Data](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) –  Jan 08 '18 at 07:05

3 Answers3

0
data = ['PROBABILITY AND STATISTICS B+', 'DISCRETE MATHEMATICS A', 'DATABASE MANAGEMENT SYSTEMS C+', 'FORMAL LANGUAGES AND AUTOMATA THEORY A']

for line in data:
     print '{:40}{:<5}'.format(' '.join(line.split()[:-1]), ' '.join(line.split()[-1:]))

$ python neat_print.py
PROBABILITY AND STATISTICS              B+
DISCRETE MATHEMATICS                    A
DATABASE MANAGEMENT SYSTEMS             C+
FORMAL LANGUAGES AND AUTOMATA THEORY    A
Chirag
  • 446
  • 2
  • 14
  • {:<5} , what does it mean? – Harry Oct 12 '17 at 14:31
  • This command reserves 5 spaces for the text that you are going to sub into it and aligns the text to the left. If you want to align center, `{:^5}`, or if you want to align right, `{:>5}`. – Chirag Oct 12 '17 at 14:38
-1

There are lots of different solutions. One of the fastest to implement might be using terminaltables. Below is an example from the official docs found here https://pypi.python.org/pypi/terminaltables

from terminaltables import AsciiTable
table_data = [
    ['Heading1', 'Heading2'],
    ['row1 column1', 'row1 column2'],
    ['row2 column1', 'row2 column2'],
    ['row3 column1', 'row3 column2']
]
table = AsciiTable(table_data)
print table.table
+--------------+--------------+
| Heading1     | Heading2     |
+--------------+--------------+
| row1 column1 | row1 column2 |
| row2 column1 | row2 column2 |
| row3 column1 | row3 column2 |
+--------------+--------------+
Igor
  • 2,834
  • 2
  • 26
  • 44
-1

Another approach using pandas

Assuming your data are as below,

import pandas as pd    

subject = ['X', 'Y', 'Z']
grade = ['A','A','B']

Reading them to a dataframe and printing

d = pd.DataFrame({'Subject': subject, 'Grade': grade})
d = d[d.columns[::-1]] #changing the order of columns
print(d.to_string(index=False)) #print without index
Subject Grade
 X     A
 Y     A
 Z     B
akilat90
  • 5,436
  • 7
  • 28
  • 42