-1

I just cannot to format of a string:

id = "3123123"
a = '{"classes": [], "field": {}}'.format(id)
print(a)

enter image description here

All suggestions will be nice !

ppostnov
  • 139
  • 9
  • 3
    `a = '{{"classes": [], "field": {}}}'.format(id)` – Shijith Apr 20 '20 at 14:53
  • 1
    Put a 0 in between the curly braces to signal, that the first parameter in format() corresponds to the first index (0) in your string. If you want to use more than one variable you can increment this number like: a = 'Some{0} is {1}'.format('thing', 'weird') Edit: To escape curly braces in a format string, use double braces like @Shijith did – DerHamm Apr 20 '20 at 14:53
  • Or: `a = f'{{"classes": [], "field": {id}}}'` – JvdV Apr 20 '20 at 14:58
  • Thanks a lot , all stuff was very useful ! – ppostnov Apr 20 '20 at 15:16

1 Answers1

3

Your formatting string clashes with the formatting placeholder specifier

You'll need to escape the { with {{

a = '{{"classes": [], "field": {{}}}}'.format(id)

And a make sure there is a placeholder for id in the formatting string with {}

rdas
  • 20,604
  • 6
  • 33
  • 46