how to formate the ouput in python

Question:

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]}tt-->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%
Asked By: Shahid

||

Answers:

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%
Answered By: CryptoFool
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.