Python string interpolation using dictionary and strings

后端 未结 8 1249
半阙折子戏
半阙折子戏 2020-12-28 15:49

Given:

dict = {\"path\": \"/var/blah\"}
curr = \"1.1\"
prev = \"1.0\"

What\'s the best/shortest way to interpolate the string to generate t

8条回答
  •  鱼传尺愫
    2020-12-28 16:26

    Why not:

    mystr = "path: %s curr: %s prev: %s" % (mydict[path], curr, prev)
    

    BTW, I've changed a couple names you were using that trample upon builtin names -- don't do that, it's never needed and once in a while will waste a lot of your time tracking down a misbehavior it causes (where something's using the builtin name assuming it means the builtin but you have hidden it with the name of our own variable).

提交回复
热议问题