Search code examples
pythondictionaryquotes

Best way to convert dictionary in string and remove quotes (" or ') around values


I have some dictionary of form:

Test = {'∧I': '([A, B] ⊦ ("A"∧B))', '∧E 1': "([(A∧B)] ⊦ 'A')", '∧E 2': '([(A∧B)] ⊦ B)'}

is their a simple way to convert it into some string without the '' around the values, means:

{'∧I': '([A, B] ⊦ ("A"∧B))', '∧E 1': ([(A∧B)] ⊦ A), '∧E 2': ([(A∧B)] ⊦ B)}

The problem with:

str1 = f'{Test}'.replace('"','')

itself is that the dictionary itself uses " or ' inside some strings.

I posted some similar post with lists. But unfortunately its a little bit difficult to transfer it on dictionaries.


Solution

  • str_ = "{" + ", ".join([f"{k}: {v}" for k, v in Test.items()]) + "}"