partial string formatting

前端 未结 21 1097
野的像风
野的像风 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:08

    If you know in what order you're formatting things:

    s = '{foo} {{bar}}'
    

    Use it like this:

    ss = s.format(foo='FOO') 
    print ss 
    >>> 'FOO {bar}'
    
    print ss.format(bar='BAR')
    >>> 'FOO BAR'
    

    You can't specify foo and bar at the same time - you have to do it sequentially.

提交回复
热议问题