Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I place variables inside of a triple-quoted string literal?

data = """
{
  "sender": "I want a variable here",
  "memo": "and here another variable",
  "amount": another variable here 
}
"""

I’ve tried using f in front of the string, and several other methods, but this is keeping me hung up.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Don’t construct JSON strings by hand as there are many intricacies of the JSON spec and your method is prone to errors. Instead, construct a dictionary first:

my_dict = {
    "sender": my_sender_variable,
    "memo": my_memo_variable,
    "amount": my_amount_variable
    }

then use the json module to convert it to a string:

my_json = json.dumps(my_dict, indent=4)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading