小编use*_*936的帖子

f 字符串中的转义字符

我遇到了以下问题f-string

>>> a='hello'

# how to print '{hello}' ?

>>> f'{{a}}'
'{a}'

>>> f'\{{a}\}'
  File "<stdin>", line 1
SyntaxError: f-string: single '}' is not allowed

# doing it without f-strings
>>> '{' + a + '}'
'{hello}'
Run Code Online (Sandbox Code Playgroud)

如何转义 fstring 中的字符?

python python-3.x f-string

4
推荐指数
1
解决办法
5804
查看次数

标签 统计

f-string ×1

python ×1

python-3.x ×1