0

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%
  • Are you using the variable cars or i[0]/i[1] ? And you can probably find the answer here: https://stackoverflow.com/questions/8450472/how-to-print-a-string-at-a-fixed-width – ScottC Oct 10 '22 at 02:59

1 Answers1

1

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%
CryptoFool
  • 21,719
  • 5
  • 26
  • 44