Python string interpolation using dictionary and strings

后端 未结 8 1239
半阙折子戏
半阙折子戏 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:12

    You can also (soon) use f-strings in Python 3.6, which is probably the shortest way to format a string:

    print(f'path: {path} curr: {curr} prev: {prev}')
    

    And even put all your data inside a dict:

    d = {"path": path, "curr": curr, "prev": prev}
    print(f'path: {d["path"]} curr: {d["curr"]} prev: {d["prev"]}')
    

提交回复
热议问题