Wild Feather
Wild Feather

Reputation: 229

How to add extra spaces before each row before printing the matrix?

I want to print the matrix named 'Transfer' using the following code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

Transfer=[[1.0,2.0],[3.0,4.0]]
s=[[str(e) for e in row] for row in Transfer]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print '\n'.join(table) 

However, the output in the terminal is aligned to the left, and I would like to add a few spaces before each row, so that the matrix looks centered. Where/how should I add the spaces in this code for it to work correctly?

Upvotes: 1

Views: 157

Answers (1)

FMc
FMc

Reputation: 42421

Center on some width:

print('\n'.join(r.center(80) for r in table))

Or just add spaces on the left:

print('\n'.join('    ' + r for r in table))

Upvotes: 1

Related Questions