2

This is what I want to replicate in Python:

These are the names of the variables that store the data:

name_1= "Alex"
name_2 ="Zia"
age_1 = 13
age_2 = 12
game_1= 1
game_2 = 2
favourite_1 ="chess"
favourite_2 = "monopoly"
cost_1= 10
cost_2 =25
total_cost = 25

I want to display this like a table, but I can't, is there a way apart from calculating the spaces between one word and the other to make it fit?

2
  • Are you asking about the GUI (graphical) aspect, or what data structure to use? Commented Mar 1, 2016 at 18:51
  • Do You want to create a table in the console? Commented Mar 1, 2016 at 18:51

3 Answers 3

2

You may use tabulate library of python for this purpose.

For example:

>>> from tabulate import tabulate
>>> value_list = [['Alex', 13,1, 'Chess', 10],
                  ['Zia',  12,2, 'Monopoly', 25]]
>>> column_list = ["Name", "Age", "Number of Games", "Favourite Game", "Cost of Game"]
>>> print tabulate(value_list, column_list, tablefmt="grid")
+--------+-------+-------------------+------------------+----------------+
| Name   |   Age |   Number of Games | Favourite Game   |   Cost of Game |
+========+=======+===================+==================+================+
| Alex   |    13 |                 1 | Chess            |             10 |
+--------+-------+-------------------+------------------+----------------+
| Zia    |    12 |                 2 | Monopoly         |             25 |
+--------+-------+-------------------+------------------+----------------+
Sign up to request clarification or add additional context in comments.

Comments

0

Use tabulate, as mentioned, or Pandas.

import pandas as pd
df = pd.DataFrame({'Name': ['Alex', 'Zia', None], 
                   'Age': [13, 12, None], 
                   'Number of games': [1, 2, None], 
                   'Favourite game': ['Chess', 'Monopoly', None], 
                   'Cost of games': [10, 25, 35]})
print(df)

Comments

0

As mentioned above you can use the tabular library like this:

from tabulate import tabulate
table=[['Alex',13,1,'Chess',10],['Zia',12,2,'Chess',25]]
headers=["Name","Age", "Number of Games","Favourite Game","Cost of Game"]
print tabulate(table, headers, tablefmt="grid")

This is what you will get:

+--------+-------+-------------------+------------------+----------------+
| Name   |   Age |   Number of Games | Favourite Game   |   Cost of Game |
+========+=======+===================+==================+================+
| Alex   |    13 |                 1 | Chess            |             10 |
+--------+-------+-------------------+------------------+----------------+
| Zia    |    12 |                 2 | Chess            |             25 |
+--------+-------+-------------------+------------------+----------------+

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.