partial string formatting

前端 未结 21 1094
野的像风
野的像风 2020-11-28 04:30

Is it possible to do partial string formatting with the advanced string formatting methods, similar to the string template safe_substitute() function?

F

21条回答
  •  醉酒成梦
    2020-11-28 05:09

    You could use the partial function from functools which is short, most readable and also describes the coder's intention:

    from functools import partial
    
    s = partial("{foo} {bar}".format, foo="FOO")
    print s(bar="BAR")
    # FOO BAR
    

提交回复
热议问题