What's wrong in this print's formatting? Why is it not alligned?

Question:

While trying to solve an exercise, I tried to make the final print’s exhibition a bit better by using tab (t) inside the line. I tried it in another stance and it made the final result look like a table.

Should I ditch this idea? What would be an alternative to improve this final line?

#this first line was given by the teacher 
vendas_produtos = [('iphone', 558147, 951642), ('galaxy', 712350, 244295), ('ipad', 573823, 26964), ('tv', 405252, 787604), ('máquina de café', 718654, 867660), ('kindle', 531580, 78830), ('geladeira', 973139, 710331), ('adega', 892292, 646016), ('notebook dell', 422760, 694913), ('notebook hp', 154753, 539704), ('notebook asus', 887061, 324831), ('microsoft surface', 438508, 667179), ('webcam', 237467, 295633), ('caixa de som', 489705, 725316), ('microfone', 328311, 644622), ('câmera canon', 591120, 994303)] 


print('ProdutottVendas de 2019ttVendas de 2020tt% de crescimento')
for tupla_produtos in vendas_produtos:
    produto, vendas2019, vendas2020 = tupla_produtos
    if vendas2020 > vendas2019:
        crescimento = vendas2020 / vendas2019 - 1
        print(f'{produto}tt{vendas2019:,.2f}tt{vendas2020:,.2f}tt{crescimento:.1%}')

Note that I tried using the t in both prints to try and make the final result aligned, like a table of sorts. The image shows what the result was like

The code and its output

Asked By: davi-s37

||

Answers:

print(f'{produto}tt{vendas2019:,.2f}tt{vendas2020:,.2f}tt{crescimento:.1%}')

The tt makes it’s job and depending on the produto length you see the unaligned result.

Try import pprint and play with that, it should give you an help!

Answered By: Alessandro Cerro

You can use string formatting like this:

vendas_produtos = [('iphone', 558147, 951642), ('galaxy', 712350, 244295), ('ipad', 573823, 26964), ('tv', 405252, 787604), ('máquina de café', 718654, 867660), ('kindle', 531580, 78830), ('geladeira', 973139, 710331), ('adega', 892292, 646016), ('notebook dell', 422760, 694913), ('notebook hp', 154753, 539704), ('notebook asus', 887061, 324831), ('microsoft surface', 438508, 667179), ('webcam', 237467, 295633), ('caixa de som', 489705, 725316), ('microfone', 328311, 644622), ('câmera canon', 591120, 994303)] 

print(f"%-20s %-20s %-20s %-20s" % ("produto", "Vendas de 2019", "Vendas de 2020", "% de crescimento") )

for tupla_produtos in vendas_produtos:
    produto, vendas2019, vendas2020 = tupla_produtos
    if vendas2020 > vendas2019:
        crescimento = vendas2020 / vendas2019 - 1
        print(f"%-20s %-20.2f %-20.2f %-20.1f" % (produto, vendas2019, vendas2020, crescimento) )

Output:

produto              Vendas de 2019       Vendas de 2020       % de crescimento    
iphone               558147.00            951642.00            0.7                 
tv                   405252.00            787604.00            0.9                 
máquina de café      718654.00            867660.00            0.2                 
notebook dell        422760.00            694913.00            0.6                 
notebook hp          154753.00            539704.00            2.5                 
microsoft surface    438508.00            667179.00            0.5                 
webcam               237467.00            295633.00            0.2                 
caixa de som         489705.00            725316.00            0.5                 
microfone            328311.00            644622.00            1.0                 
câmera canon         591120.00            994303.00            0.7                 
Answered By: AziMez