Test
Test

Reputation: 550

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

_________________________________________________________________
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
_________________________________________________________________

Upvotes: 0

Views: 691

Answers (2)

vladsiv
vladsiv

Reputation: 2946

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              
_________________________________________________________________

Upvotes: 1

Kiavash Ferdousi
Kiavash Ferdousi

Reputation: 11

hi so you have to remove some \t cause they create tabs and you can remove them one by one to find one that you prefer

print('for example, this is a tab \t\t\t there is going to be space between them')

print('for example, there is no tab here and it is going to be next to each other')

Upvotes: 1

Related Questions