如何在python的字元串中輸入純粹的{

  • 2020 年 1 月 19 日
  • 筆記

python的format函數通過{}來格式化字元串

>>> a='{0}'.format(123)  >>> a  '123'

如果需要在文本中包含{}字元,這樣使用就會報錯:

>>> a='{123} {0}'.format('123')  Traceback (most recent call last):    File "<stdin>", line 1, in <module>  IndexError: tuple index out of range

需要通過{{}},也就是double的{}來進行轉義

>>> a='{{123}} {0}'.format('123')  >>> a  '{123} 123'

參考鏈接:

    https://docs.python.org/3/library/string.html#formatstrings