如何在python的字符串中输入纯粹的{}

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

posted @ 2018-08-22 16:48  丁壮  阅读(355)  评论(0编辑  收藏  举报