Skip to content

Instantly share code, notes, and snippets.

@epifanio
Created October 5, 2019 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epifanio/98698db4f23a7cc9f4b97d2cb1d13ba7 to your computer and use it in GitHub Desktop.
Save epifanio/98698db4f23a7cc9f4b97d2cb1d13ba7 to your computer and use it in GitHub Desktop.
name = 'n'
type = 't'
stored = 'true'
data = '{\n "add-field":{\n "name":"%s",\n "type":"%s",\n "stored":%s }\n}' % (name, type, stored)
data
@epifanio
Copy link
Author

epifanio commented Oct 5, 2019

attempt to use f-string:

data = f'{\n  "add-field":{\n     "name":"{name}",\n     "type":"{type}",\n     "stored":{stored} }\n}'

return the error:

  File "<ipython-input-24-2c1b73586698>", line 1
    data = f'{\n  "add-field":{\n     "name":"{name}",\n     "type":"{type}",\n     "stored":{stored} }\n}'
          ^
SyntaxError: f-string expression part cannot include a backslash

@epifanio
Copy link
Author

epifanio commented Oct 5, 2019

while using str.format() :

data = '{\n  "add-field":{\n     "name":"{name}",\n     "type":"{type}",\n     "stored":{stored} }\n}'.format(name=name, type=type, stored=stored)

i got:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-26-1c58c43a20c5> in <module>
----> 1 data = '{\n  "add-field":{\n     "name":"{name}",\n     "type":"{type}",\n     "stored":{stored} }\n}'.format(name=name, type=type, stored=stored)

KeyError: '\n  "add-field"'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment