>>b="thisisb">>>f"{aif10>5elseb}"'thisisa'>>>f"{aif1"/>
当前位置:  首页>> 技术小册>> Python合辑5-格式化字符串

F-string也能计算稍微复杂的运算式,比如if/else

  1. >>> a = "this is a"
  2. >>> b = "this is b"
  3. >>> f"{a if 10 > 5 else b}"
  4. 'this is a'
  5. >>> f"{a if 10 < 5 else b}"
  6. 'this is b'