partial string formatting

前端 未结 21 1017
野的像风
野的像风 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 04:57

    A very ugly but the simplest solution for me is to just do:

    tmpl = '{foo}, {bar}'
    tmpl.replace('{bar}', 'BAR')
    Out[3]: '{foo}, BAR'
    

    This way you still can use tmpl as regular template and perform partial formatting only when needed. I find this problem too trivial to use a overkilling solution like Mohan Raj's.

提交回复
热议问题