-1

I'm trying to use the format string from python with this.

values = f'''{
          "sourceId": "{merchant['siteId']}",
          "programId": "{merchant['id']}"
        }'''

but I'm getting invalid format specifier. any idea how to use it correctly in this situation?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

You just need to add one more curly braces to make it work as a normal string

values = f'''{{
  "sourceId": "{merchant['siteId']}",
  "programId": "{merchant['id']}"
}}'''

For example:

merchant = {
    'siteId': 1,
    'id': 2
}

values = f'''{{
  "sourceId": "{merchant['siteId']}",
  "programId": "{merchant['id']}"
}}'''
print(values)

Outputs:

{
  "sourceId": "1",
  "programId": "2"
}
Ghost Ops
  • 1,710
  • 2
  • 13
  • 23