python, format string

后端 未结 8 1734
野性不改
野性不改 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条回答
  •  旧时难觅i
    2020-12-28 18:31

    %% escapes the % symbol. So basically you just have to write:

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

    And if ever you need to output a percentage or something:

    >>> "%s %s %%%s" % ('foo', 'bar', '10')
    'foo bar %10'
    

提交回复
热议问题