Search code examples
pythonstringformattingf-string

Format float while printing dict


Is there any possibility to easily format float like this:

f"{value: .2f}"

while printing dict, that contains those floats as values? This doesn't work, but I'm looking for something equivalent:

f"{dictionary: .2f}"

Solution

  • You can loop on the keys of the dictionary to print each value (or combine them into one string to print). You can loop on each key with :

    for key in dictio.keys():
       print (key, "{:.2f}".format(dictio[key]))