How can I print literal curly-brace characters in python string and also use .format on it?

前端 未结 16 1640
面向向阳花
面向向阳花 2020-11-21 05:31
x = \" \\{ Hello \\} {0} \"
print(x.format(42))

gives me : Key Error: Hello\\\\

I want to print the output: {Hello} 42

16条回答
  •  梦毁少年i
    2020-11-21 05:46

    If you want to only print one curly brace (for example {) you can use {{, and you can add more braces later in the string if you want. For example:

    >>> f'{{ there is a curly brace on the left. Oh, and 1 + 1 is {1 + 1}'
    '{ there is a curly brace on the left. Oh, and 1 + 1 is 2'
    

提交回复
热议问题