python print table
need to take some comma separated data and turn it into a table format, in python?
we can do this easily using the format method.
format method syntax
result = template.format(_*_parameters)
ELI5
result: result is just a variable. it's where you will store the output.
template.format: template.format is what you use to invoke the format method on python template class for the table you're about to make - it defines your columns.
(*parameters): (*parameters) is where you will put an infinite amount of arguments telling python what you want it to do.
- for example, you could write: template.format(revenue="revenue," profit="profit," percent="profit/revenue)" and this will tell python:
- for the first argument i passed in, please use the string "revenue" as the
example: turning data into a table in python
let's say you have some data for your company's revenue, and profit, like so:
1100,11 2200,19 2400,110 2300,-112
this is how you would take that data and turn it into a table in python:
-
enter your data
-
add a header row to the table (optional)
-
format the data
-
print the data into rows
#1 the data
data = [
(1100,11),
(2200,19),
(2400,110),
(2300,-112),
]
#2 add a header row to the table
print("Revenue | Profit | Percent Change")
#3 format the data
#4 print the data into rows
for revenue, profit in data:
row = template.format(revenue=revenue, profit=profit, percent=profit/revenue)
print(row)
output
Revenue | Profit | Percent Change
1,100 | +11 | 1%
2,200 | +19 | 1%
2,400 | +110 | 5%
2,300 | -112 | -5%
tags:
-
how to print data in table format in python
-
python print table
-
python create table
-
print a list
-
how to make a table in python
-
how to print a list in python
-
make a table in python
-
python data table
-
creating a table in python
-
how to create a table in python