0

I'm using python formating to print some left aligned columns.

This nicely left alignes a, b and c:

out_string = '{:30} {:20} {:10}'.format(a, b, c)

Then I tried using constants, but failed with ValueError: Invalid format specifier:

ALIGNa = 30
ALIGNb = 20
ALIGNc = 10
out_string = '{:ALIGNa} {:ALIGNb} {:ALIGNc}'.format(a, b, c)

Why does it fail?

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • Is there any reason why you can't use an f-string? – DarkKnight Oct 19 '22 at 07:23
  • You can find the explanation [here](https://stackoverflow.com/questions/29044940/how-can-you-use-a-variable-name-inside-a-python-format-specifier). Try formatting with fstring- ```out_string = f'{a[:ALIGNa]} {b[:ALIGNb]} {c[:ALIGNc]}'``` – ishmam Oct 19 '22 at 07:30

0 Answers0