>>f"{{hello}}={hello}"'{hello}=world'``"/>
当前位置:  首页>> 技术小册>> Python合辑5-格式化字符串

如果想打印由大括号括起来的变量名称,而不是该变量的值,那就需要双层大括号 {{}}。

  1. >>> hello = "world"
  2. >>>f"{{hello}} = {hello}"
  3. '{hello} = world'

而如果想转义双引号,就需要在引号前用反斜线 \ 做标记。

  1. >>>f"{hello} = \"hello\""
  2. 'world = "hello"'

该分类下的相关小册推荐: