python, format string

后端 未结 8 1765
野性不改
野性不改 2020-12-28 18:00

I am trying to build a format string with lazy argument, eg I need smth like:

\"%s \\%s %s\" % (\'foo\', \'bar\') # \"foo %s bar\"

how can

8条回答
  •  醉酒成梦
    2020-12-28 18:26

    Python 3.6 now supports shorthand literal string interpolation with PEP 498. For your use case, the new syntax allows:

    var1 = 'foo'
    var2 = 'bar'
    print(f"{var1} %s {var2}")
    

提交回复
热议问题