Home > Enterprise >  How can I align columns vertically in python?
How can I align columns vertically in python?

Time:12-01

print(f'{"Progress":2} {"Trailer":3} {"Retriver":4} {"Excluded":5}')
print(f'{"*":2} {"*":3} {"*":4} {"*":5}')

The print function prints it as:

Progress Trailer Retriver Excluded
*  *   *    *

How can I align the star signs under the right column?

CodePudding user response:

Try the below code, it will assign fixed column width and center the words:

print(f'{"Progress":^10} {"Trailer":^10} {"Retriver":^10} {"Excluded":^10}')
print(f'{"*":^10} {"*":^10} {"*":^10} {"*":^10}')

It will give output as below:

 Progress   Trailer    Retriver   Excluded 
    *          *          *          *     

CodePudding user response:

Fix the column widths:

print(f'{"Progress":<20} {"Trailer":<20} {"Retriver":<20} {"Excluded":<20}')
print(f'{"*":<20} {"*":<20} {"*":<20} {"*":<20}')
Output:
Progress             Trailer              Retriver             Excluded            
*                    *                    *                    *  

If you would like to right-align instead, change the <20 to >20

  • Related