Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Beautification of the print output of the console

I have an output in the Console. Unfortunately the texts are of different length and therefore looks very shifted. Is there an option that writes the texts below each other, no matter how many characters are in front of them, so that the output looks the way I want it to look?

I would not like to use another library for this.

print(65 * '_')
print('algorithm\t\t\tsil\t\tdbs')

results =  ['Agglomerative Clustering', 0.8665, 0.4200]
formatter_result = ("{:9s}\t\t{:.4f}\t{:.4f}")
print(formatter_result.format(*results))
    
results = ['k-Means', 0.9865, 0.1200]
formatter_result = ("{:9s}\t\t{:.4f}\t{:.4f}")
print(formatter_result.format(*results))

print(65 * '_')

What I have

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

_________________________________________________________________
algorithm           sil     dbs
Agglomerative Clustering        0.8665  0.4200
k-Means         0.9865  0.1200
_________________________________________________________________

What I want

_________________________________________________________________
algorithm                    sil        dbs
Agglomerative Clustering     0.8665     0.4200
k-Means                      0.9865     0.1200
_________________________________________________________________

I looked at Printing Lists as Tabular Data and tried it, but dosen’t work for me

print(65 * '_')

heading = ['algorithm', 'sil', 'dbs']
result1 =  ['Agglomerative Clustering', 0.8665, 0.4200]
result2 = ['k-Means', 0.9865, 0.1200]
ab = np.array([heading, result1, result2])
for row in ab:
    print("{: >20} {: >20} {: >20}".format(*row))
    



print(65 * '_')
_________________________________________________________________
           algorithm                  sil                  dbs
Agglomerative Clustering               0.8665                 0.42
             k-Means               0.9865                 0.12
_________________________________________________________________

>Solution :

You could use {:<20}, this means 20 spaces with left alignment. There is also {:^20} – center alignment and {:>20}– right alignment.

To combine it with float notation e.g. .4f just add to it: {:<20.4f}

Try this:

print(65 * "_")
formatter_result = "{:<35} {:<20} {:<20}"
formatter_result_f = "{:<35} {:<20.4f} {:<20.4f}"
print(formatter_result.format(*["algorithm", "sil", "dbs"]))
results = ["Agglomerative Clustering", 0.8665, 0.4200]
print(formatter_result_f.format(*results))
results = ["k-Means", 0.9865, 0.1200]
print(formatter_result_f.format(*results))
print(65 * "_")

Result:

_________________________________________________________________
algorithm                           sil                  dbs                 
Agglomerative Clustering            0.8665               0.4200              
k-Means                             0.9865               0.1200              
_________________________________________________________________
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading