Format string in python with variable formatting
问题 How can I use variables to format my variables? cart = {"pinapple": 1, "towel": 4, "lube": 1} column_width = max(len(item) for item in items) for item, qty in cart.items(): print "{:column_width}: {}".format(item, qty) > ValueError: Invalid conversion specification or (...): print "{:"+str(column_width)+"}: {}".format(item, qty) > ValueError: Single '}' encountered in format string What I can do, though, is first construct the formatting string and then format it: (...): formatter = "{:"+str