Home > OS >  how to formate the ouput in python
how to formate the ouput in python

Time:10-10

I'm working on a python assignment and want the output separated by the same spaces, the code I'm running is:

    for cars in car_percentage:
    print(f"{i[0]}\t\t-->\t{i[1]}")

this results in the output:

Tesla Model S Performance       --> 68%
Volkswagen ID.3 77 kWh Tour     --> 70%
Tesla Model 3 LR 4WD        --> 71%
Tesla Model 3 Performance       --> 75%
Tesla Model Y LR 4WD        --> 79%

while I want the output to have the same spaces no matter how long the car name is, just like:

Tesla Model S Performance       --> 68%
Volkswagen ID.3 77 kWh Tour     --> 70%
Tesla Model 3 LR 4WD            --> 71%
Tesla Model 3 Performance       --> 75%
Tesla Model Y LR 4WD            --> 79%

CodePudding user response:

You just need to add a padding format specifier, like this:

car_percentage = (
    ("Tesla Model S Performance", 68),
    ("Volkswagen ID.3 77 kWh Tour", 70),
    ("Tesla Model 3 LR 4WD", 71),
    ("Tesla Model 3 Performanc", 75),
    ("Tesla Model Y LR 4WD", 79),
)

for car in car_percentage:
    print(f"{car[0]:32} --> {car[1]}%")

Result:

Tesla Model S Performance        --> 68%
Volkswagen ID.3 77 kWh Tour      --> 70%
Tesla Model 3 LR 4WD             --> 71%
Tesla Model 3 Performance        --> 75%
Tesla Model Y LR 4WD             --> 79%
  • Related