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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2021-08-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2021-08-25
  • 2022-12-23
相关资源
相似解决方案