Hallo Zusammen,
gibt es eine einfache Möglichkeit, die minimumwidth von format variabel anzugeben?
Mit starrer Angabe:
Python
dict_ = {"abc": 1, "abcdefg": 2}
text = "Überschrift:\n"
for key, value in dict_.items():
text = text + "{:15}: {}\n".format(key, value)
print(text)
So wie ich es mir vorstellen würde, aber nicht Funktioniert:
Python
dict_ = {"abc": 1, "abcdefg": 2}
zeichenanzahl = max([len(wert) for wert in dict_.keys()])
text = "Überschrift:\n"
for key, value in dict_.items():
text = text + "{:zeichenanzahl}: {}\n".format(key, value)
print(text)
Traceback:
Code
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-cd8ea6a4f3ed> in <module>()
4 text = "Überschrift:\n"
5 for key, value in dict_.items():
----> 6 text = text + "{:zeichenanzahl}: {}\n".format(key, value)
7 print(text)
ValueError: Invalid format specifier